<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Tangent Space</title>
    <description></description>
    <link>http://charlesfrye.github.io/</link>
    <atom:link href="http://charlesfrye.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sat, 26 Oct 2024 02:05:09 +0000</pubDate>
    <lastBuildDate>Sat, 26 Oct 2024 02:05:09 +0000</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
      <item>
        <title>Reckoner Bundles: A Riddler&apos;s Inway</title>
        <description>&lt;p&gt;&lt;em&gt;This gloss of a key passage from “Computer Systems: A Programmer’s Approach” is inspired by Poul Anderson’s
&lt;a href=&quot;https://charlesfrye.github.io/pdfs/uncleftish-beholding.pdf&quot;&gt;“Uncleftish Beholding”&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A reckoner bundle is made of hardware and bundlish software
that work together to run wending riddlelists.&lt;/p&gt;

&lt;p&gt;Enlightenment within the reckoner is forstood as clusters of twines
(short for &lt;em&gt;two rimes&lt;/em&gt;)
that are understood in unlike ways,
depending on the background.&lt;/p&gt;

&lt;!--exc--&gt;

&lt;p&gt;Riddlelists are rewoven by other riddlelists into unlike shapes,
beginning as ASCII scribes and then rewoven by builders and linkers
into twoname runnables.&lt;/p&gt;

&lt;p&gt;Flowmakers read and plead twoname riddles from the main wordhold.&lt;/p&gt;

&lt;p&gt;As reckoners spend most of their time
doubling givens twixt wordhold, input and output workthings,
and the middle flowmaker (MFM) logs,
the storage workthings in a bundle are laid out in a rank-listing,
with the MFM logs at the top,
followed by many ranks of hardware hoard wordholds,
main wordhold, and ring storage.&lt;/p&gt;

&lt;p&gt;Storage workthings that are higher in the rank-listing are
faster and more dear per twine than those lower in the rank-listing.&lt;/p&gt;

&lt;p&gt;Riddlers can bettermake the performance of their C riddlelists
by kenning well and enplundering the wordhold rank-listing.&lt;/p&gt;

&lt;p&gt;The runwarden kernel works as a go-between twixt
the wending and the hardware.&lt;/p&gt;

&lt;p&gt;It puts forth three groundish engrippings:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Binders are engrippings for I/O workthings.&lt;/li&gt;
  &lt;li&gt;Craftwordhold is an engripping for both main wordhold and rings.&lt;/li&gt;
  &lt;li&gt;Flows are engrippings for the flowmaker, main wordhold, and I/O workthings.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Finally, networks put forth ways for reckoner bundles to speak
with one another.&lt;/p&gt;

&lt;p&gt;From the seepoint of a lone bundle,
the network is just another I/O workthing.&lt;/p&gt;
</description>
        <pubDate>Fri, 25 Oct 2024 00:00:00 +0000</pubDate>
        <link>http://charlesfrye.github.io/programming/2024/10/25/reckoner-riddlelisting.html</link>
        <guid isPermaLink="true">http://charlesfrye.github.io/programming/2024/10/25/reckoner-riddlelisting.html</guid>
        
        
        <category>programming</category>
        
      </item>
    
      <item>
        <title>At the Intersection of LLMs and Kernels - Research Roundup</title>
        <description>&lt;p style=&quot;text-align: center&quot;&gt;&lt;a href=&quot;https://twitter.com/karpathy/status/1707437820045062561&quot;&gt;&lt;img src=&quot;https://charlesfrye.github.io/img/karpathy-llm-kernel-tweet.png&quot; alt=&quot;karpathy-llm-kernel-tweet&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Relational databases like postgreSQL
and operating system kernels like Linux
are foundational building blocks for computing systems.
Every “application” is in essence an “application”
of those building blocks to some purpose:
Twitter applies databases to write and read tweets;
it applies an operating system
to operate the hardware required to do so.&lt;/p&gt;

&lt;p&gt;Large language models –
and large generative models of complex data types more broadly –
are a new building block at this foundational layer.
ML researchers like me have many useful intuitions about
what can be built with this novel building block and how to improve it.&lt;/p&gt;

&lt;p&gt;But the people who have spent years building
with those other blocks have many more.
Many of these inuitions are surprising to someone who has spent more time
with neural networks than with the network stack.&lt;/p&gt;

&lt;p&gt;This post is a collection of short explainers for papers
that directly draw from systems metaphors
in designing improvements for LLMs,
ranging from novel approaches to pretraining
to inference-time speed optimizations.&lt;/p&gt;

&lt;p&gt;Perhaps most interesting are prompting strategies that make LLMs act more like kernels.&lt;/p&gt;

&lt;p&gt;It is intended to get researchers and hackers of LLMs
interested in systems
and vice versa.&lt;/p&gt;

&lt;!--exc--&gt;

&lt;p&gt;We’ll proceed by first introducing an idea from
the systems level –
roughly, the interface of hardware, kernel, and application –
and then showing how it has been applied to LLMs
in recent research.&lt;/p&gt;

&lt;h4 id=&quot;update-2023-12-30-the-topics-discussed-here-are-also-covered-in-this-recorded-talk-from-the-scale-by-the-bay-2023-conference&quot;&gt;Update 2023-12-30: the topics discussed here are also covered in &lt;a href=&quot;https://www.youtube.com/watch?v=VxFtHqlMv8c&quot;&gt;this recorded talk from the &lt;em&gt;Scale By The Bay 2023&lt;/em&gt; conference&lt;/a&gt;.&lt;/h4&gt;

&lt;h2 id=&quot;speculative-execution&quot;&gt;Speculative Execution&lt;/h2&gt;

&lt;h3 id=&quot;speculative-execution-in-processors&quot;&gt;Speculative Execution in Processors&lt;/h3&gt;

&lt;p&gt;Sometimes bottlenecks are mitigated by executing
the work that &lt;em&gt;most likely&lt;/em&gt; follows them:
we “speculatively execute”
work that will probably, but not necessarily,
come after our bottleneck.&lt;/p&gt;

&lt;p&gt;For example, rather than waiting for the result of a conditional
to be available in order to determine which branch to execute,
the processor could simply proceed with the “most likely” branch,
based on compile-time hints or runtime statistics.
This particular technique for speculative execution is called “branch prediction”.&lt;/p&gt;

&lt;p&gt;This can lead to large speedups when a particular branch
of a conditional is taken only rarely
(e.g. unhappy/error-handling paths)
and requires a lot of work to execute
(e.g. is subject to cache misses).&lt;/p&gt;

&lt;p&gt;Speculative execution is infamous
thanks to the
&lt;a href=&quot;https://arxiv.org/abs/1801.01203&quot;&gt;Spectre&lt;/a&gt;
vulnerability,
which manipulates runtime statistics to
“trick” the processor into executing
out-of-bounds memory reads.&lt;/p&gt;

&lt;p&gt;Undaunted by that cautionary tale of a
&lt;a href=&quot;https://www.statista.com/statistics/800258/worldwide-meltdown-spectre-potential-mitigation-cost-by-device-type/&quot;&gt;multi-billion-dollar vulnerability&lt;/a&gt;,
let’s consider how we might replicate
this pattern of
executing cheap but possibly wrong operations
to speed up LLM inference.&lt;/p&gt;

&lt;h3 id=&quot;speculative-execution-in-llms-accelerating-large-language-model-decoding-with-speculative-sampling-and-fast-inference-from-transformers-via-speculative-decoding&quot;&gt;Speculative Execution in LLMs: &lt;a href=&quot;https://arxiv.org/abs/2302.01318&quot;&gt;Accelerating Large Language Model Decoding with Speculative Sampling&lt;/a&gt; and &lt;a href=&quot;https://arxiv.org/abs/2211.17192&quot;&gt;Fast Inference from Transformers via Speculative Decoding&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Two roughly-simultaneous papers from
Google Research and DeepMind,
perennial frenemies,
demonstrated roughly-identical methods for large speedups in LLM generation
by using cheaper models to predict certain tokens
without changing the system’s output probabilities.
In essence,
speculatively generating a number of likely output tokens
before the actual LLM is used to compute the final outputs.&lt;/p&gt;

&lt;p&gt;The intuition behind speculative sampling is that
some of the tokens in the output are very easy to guess –
a period will come at the end of this sentence, for example –
and so can be predicted very cheaply.&lt;/p&gt;

&lt;p&gt;These methods further take advantage of the fact that
computing the logprobs for
a prompt + K tokens can be done in parallel.
That makes it much cheaper than sampling K tokens to follow a prompt,
which must be done serially –
a painful and unavoidable fact of autoregressive models like Transformers or RNNs.&lt;/p&gt;

&lt;p&gt;The K tokens following the prompt are generated by some cheap strategy –
a smaller language model or even a heuristic rule.
We then compute the logprobs for the prompt + K tokens
in the large model, in parallel,
and then throw out only those tokens that are improbable
according to the large model.&lt;/p&gt;

&lt;p&gt;Importantly, we reject samples
in direct proportion to how improbable the large model thinks they are,
so the combined system has the exact same output behavior as the original large model,
but generally runs faster.
This is a classic trick,
rejection sampling, that
&lt;a href=&quot;https://www.jstor.org/stable/2005864&quot;&gt;goes back&lt;/a&gt;
to the very first (analog!) computers in the 1940s.&lt;/p&gt;

&lt;p&gt;For temperature 0, aka deterministic sampling,
this equates to throwing out all tokens that follow
the first wrong token put out by the speculative process.&lt;/p&gt;

&lt;p&gt;The basic flow is captured well
in this animation of speculative sampling from
&lt;a href=&quot;https://huggingface.co/blog/assisted-generation&quot;&gt;an excellent explainer by Joao Gante of Hugging Face&lt;/a&gt;:&lt;/p&gt;

&lt;video style=&quot;width: 90%&quot; autoplay=&quot;&quot; loop=&quot;&quot; muted=&quot;&quot; playsinline=&quot;&quot; src=&quot;https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/assisted-generation/gif_4_1080p.mov&quot;&gt;&lt;/video&gt;

&lt;p&gt;As with branch prediction, we do some work that might be wasted:
running the draft/speculative model
and scoring logprobs on all K tokens.&lt;/p&gt;

&lt;p&gt;But because running the model on the prompt + K tokens is roughly the same cost
as executing on the prompt + 1 token, for small K,
we almost always come out ahead –
much as branch predicting speculative execution comes out ahead because
reading from memory and then executing the right branch
is roughly the same cost as executing a wrong branch while reading from memory and then executing the right branch.&lt;/p&gt;

&lt;p&gt;When used in a hierarchical configuration –
regexes speculating for BERTs that speculate for GPTs –
speculative sampling can achieve
2x speedups or greater.&lt;/p&gt;

&lt;p&gt;Note that 2-3x is the typical cost difference between
prompt tokens and output tokens in LLM APIs,
and so well-executed speculative decoding
can reduce that cost difference to almost nothing
(thanks to
&lt;a href=&quot;https://twitter.com/moinnadeem&quot;&gt;Moin Nadeem&lt;/a&gt;
of
&lt;a href=&quot;https://replicate.ai/&quot;&gt;Replicate&lt;/a&gt;
for thoughtful discussions on this).&lt;/p&gt;

&lt;h2 id=&quot;registers&quot;&gt;Registers&lt;/h2&gt;

&lt;h3 id=&quot;registers-in-processors&quot;&gt;Registers in Processors&lt;/h3&gt;

&lt;p&gt;Programs generally achieve their effects on the world
by writing information to memory visible to other processes or hardware –
RAM or disk for other processes,
VRAM for the display,
network sockets for other computers, etc.
Registers store small amounts of intermediate data
during the computation of this information.&lt;/p&gt;

&lt;p&gt;Unlike stack-allocated or heap-allocated memory,
these values are stored in the processor itself
and are essentially inaccessible to the programmer
(without assembly-level backdoors)
in high-level languages like C.&lt;/p&gt;

&lt;p&gt;Because they are abstracted away from the programmer,
they can be manipulated – e.g. by a compiler when optimizing the program – without concern
for the side effects of those manipulations.&lt;/p&gt;

&lt;p&gt;How might this pattern show up in LLMs?&lt;/p&gt;

&lt;h3 id=&quot;registers-in-large-transformer-models-vision-transformers-need-registers&quot;&gt;Registers in Large Transformer Models: &lt;a href=&quot;https://arxiv.org/abs/2309.16588&quot;&gt;Vision Transformers Need Registers&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Alright, this is cheating a bit, because it’s Vision Transformers,
rather than Transformer language models,
but it’s still a cool paper so I’m going to explain it.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;
There’s also a weaker but similar result for LLMs,
based on
&lt;a href=&quot;https://arxiv.org/abs/2310.02226&quot;&gt;“pause tokens”&lt;/a&gt;.
&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;This paper claims that&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Vision Transformers use uninformative pixels
as dynamic registers for storing intermediate information&lt;/li&gt;
  &lt;li&gt;You can intentionally add empty “register tokens” to the input to remove this behavior&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first point is backed up by this gorgeous figure:&lt;/p&gt;

&lt;p style=&quot;text-align: center&quot;&gt;&lt;img src=&quot;https://charlesfrye.github.io/img/vision-transformers-need-registers.png&quot; alt=&quot;vision-transformers-need-registers&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The bright colors indicate areas of the image to which the models attended
when producing their predictions.
Notably, large ViTs attend heavily to
uninformative, background pixels.&lt;/p&gt;

&lt;p&gt;Does this mean the models are learning to use artifacts of the dataset
or adversarially-attackable side channel information to make their predictions?&lt;/p&gt;

&lt;p&gt;Not necessarily!&lt;/p&gt;

&lt;p&gt;Because each layer interacts additively
with its inputs –
the output of a layer with input &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; can be written as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f(x) + x&lt;/code&gt; –
Transformer layers can treat their inputs
like a content-addressable memory,
reading information written by earlier layers and writing in some of their own.
For more on this view of Transformer internals, see the excellent
&lt;a href=&quot;https://transformer-circuits.pub&quot;&gt;work from Anthropic’s interpretability team&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It’s a neat way to do computation,
but it has a big issue:
if you want to write information that’s not tied to a specific token position,
you don’t have a good place to put it.&lt;/p&gt;

&lt;p&gt;It is hypothesized in the paper that large ViTs learn to use
the least-informative token areas as a kind of scratchpad –
like stealing a block of memory,
using it to store intermediate values,
and hoping you don’t break anything.&lt;/p&gt;

&lt;p&gt;The paper proposes adding some blank “register” tokens
to the input in order to give the model a safe spot to store
the results of calculations that don’t otherwise have a place to go.&lt;/p&gt;

&lt;p&gt;The addition of registers does not meaningfully improve performance on benchmarks
(cf. Table 2 in &lt;a href=&quot;https://arxiv.org/abs/2309.16588&quot;&gt;the paper&lt;/a&gt;),
but the resulting attention maps are much cleaner,
as is obvious from the figure above,
and so one might expect some downstream benefits,
e.g. to interpretability or steerability.&lt;/p&gt;

&lt;h2 id=&quot;paged-memory&quot;&gt;Paged Memory&lt;/h2&gt;

&lt;h3 id=&quot;paged-memory-in-processors&quot;&gt;Paged Memory in Processors&lt;/h3&gt;

&lt;p&gt;Memory must be allocated, and those allocations must be tracked and managed,
e.g. defragmented, to maximize utilization of memory resources,
which were precious even before
&lt;a href=&quot;https://knowyourmeme.com/memes/google-chrome-ram-hog&quot;&gt;the Chrome Nation attacked&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Memory allocators must handle a gnar trade-off:
address-wise allocations make efficient use of memory in the best case,
but lead to fragmentation and expensive allocator operations in reality,
while large per-process/thread allocations are easy to manage but lead to
wasted memory in the generic case that the allocations are not fully utilized.
This particular form of under-utilization is
called “internal fragmentation”.&lt;/p&gt;

&lt;p&gt;The solution is to split the difference and allocate memory in
multiples of moderately-sized blocks called “pages”.&lt;/p&gt;

&lt;p&gt;The diagram below, from the
&lt;a href=&quot;https://os.phil-opp.com/&quot;&gt;BlogOS series by phil-opp&lt;/a&gt;,
depicts the difference between fixed-size block allocation
(left) and paged allocation (right).
Notice that the allocation for the process in blue fails in the block allocation case
but succeeds with pages.&lt;/p&gt;

&lt;p style=&quot;text-align: center&quot;&gt;&lt;img src=&quot;https://charlesfrye.github.io/img/paged-memory-blogos.png&quot; alt=&quot;paged-memory-blogos&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Pages can be assigned to processes,
loaded into caches,
and otherwise managed as bulk units,
while still being small enough
(4 KB by default in most Linuxes)
to be efficiently utilized.&lt;/p&gt;

&lt;p&gt;LLMs are memory intensive,
so might we be able to improve their performance by designing a better allocator?&lt;/p&gt;

&lt;h3 id=&quot;paged-memory-in-llms-efficient-memory-management-for-large-language-model-serving-with-pagedattention&quot;&gt;Paged Memory in LLMs: &lt;a href=&quot;https://arxiv.org/abs/2309.06180&quot;&gt;Efficient Memory Management for Large Language Model Serving with PagedAttention&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;The focus of memory management in LLM inference is on the
key-value (“KV”) cache,
used to store the intermediate values of attention computations,
for re-use when computing the outputs for future tokens in the sequence.
This cache,
introduced by the 2022 paper
&lt;a href=&quot;https://arxiv.org/abs/2211.05102&quot;&gt;Efficiently Scaling Tansformer Inference&lt;/a&gt;
from Google,
converts a quadratic-time operation (compute attention for all token pairs in the sequence)
into a linear-time one (compute only attention for the newest token in the sequence),
at a linear cost in space (store keys and values for each token in the sequence).&lt;/p&gt;

&lt;p&gt;&lt;small&gt;
If hot-swappable LoRA adapters ever take off,
a similar amount of memory legerdemain will be required
for the weights as well.
&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;The KV cache is typically allocated as a single large block of memory,
which is subject to the same issues as large block allocations in general.&lt;/p&gt;

&lt;p&gt;This problem is especially acute for batched requests to LLM services.
Because requests from different clients have varying prompt lengths
and varying response lengths
that are not known until run time,
the utilization of the KV cache becomes highly uneven.&lt;/p&gt;

&lt;p&gt;The situation is depicted nicely in this figure from the paper
that introduced PagedAttention and the vLLM inference server
that uses it.
Note that the “internal fragmentation” sections
should be several hundred times larger than they are depicted here!&lt;/p&gt;

&lt;p style=&quot;text-align: center&quot;&gt;&lt;img src=&quot;https://charlesfrye.github.io/img/paged-attention-frag.png&quot; alt=&quot;paged-attention-frag&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The solution proposed by the paper is to allocate the KV cache
via pages – each one with enough space to hold a handful of KV states –
and use a page table, much like the one in a processor,
to map the logical memory of the cache
to its physical address.&lt;/p&gt;

&lt;p&gt;Like this:&lt;/p&gt;

&lt;p style=&quot;text-align: center&quot;&gt;&lt;img src=&quot;https://charlesfrye.github.io/img/paged-attention-table.png&quot; alt=&quot;paged-attention-table&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The vLLM inference server that uses this technique
is much faster than peer systems
and allocates far less memory to serve the same workloads.&lt;/p&gt;

&lt;h2 id=&quot;virtual-memory&quot;&gt;Virtual Memory&lt;/h2&gt;

&lt;h3 id=&quot;virtual-memory-in-processors&quot;&gt;Virtual Memory in Processors&lt;/h3&gt;

&lt;p&gt;In the beforetimes,
when metal was metal,
programmers stored information at addresses with names like
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0BFF&lt;/code&gt;
and they really meant it.&lt;/p&gt;

&lt;p&gt;They could expect that if they took their trusty oscilloscope to the
3,071st/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0BFF&lt;/code&gt;th
memory cell of their spacious 64KB of RAM,
they would find humming electrons vibing to the pattern they had stored.&lt;/p&gt;

&lt;p&gt;This has an obvious problem:
if two 00s tween programmers attempt to use the one and only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0BFF&lt;/code&gt; address
to store program data at the same time,
someone’s data will be overwritten or read improperly –
akin to the problems of addressing more than one person as your BFF.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;
Larger address spaces allow for the definition of additional BFFs, e.g.
&lt;a href=&quot;https://www.urbandictionary.com/define.php?term=bffaeae&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0BFFAEAE&lt;/code&gt;&lt;/a&gt;
in 32 bit address spaces,
but this only reduces the chance of collision without eliminating it.
&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;The solution is to use a virtual memory system:
programmers and their programs are presented with a gorgeous row
of linear addresses,
from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00000000&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xFFFFFFFF&lt;/code&gt;
(or whatever the highest address is),
and the operating system and hardware map those addresses to physical memory.&lt;/p&gt;

&lt;p&gt;This indirection is also useful for pages!
Instead of mapping individual addresses,
virtual memory maps typically operate at the page level.&lt;/p&gt;

&lt;p&gt;Virtual memory’s indirection allows a process to address much more
than the available RAM –
for example virtual memory addresses can be mapped to locations on disk
or on &lt;a href=&quot;https://aws.amazon.com/s3/&quot;&gt;somebody else’s computer&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;RAM is limited and expensive, relative to disk,
so being able to use disk as memory is a big win.&lt;/p&gt;

&lt;p&gt;Language models also have memory limits:
when producing tokens,
they can only refer to at most a fixed number of previous context tokens.&lt;/p&gt;

&lt;p&gt;Hard limits on token count have rapidly increased from the few thousands to the few hundreds of thousands.
But
&lt;a href=&quot;https://arxiv.org/abs/2307.03172&quot;&gt;models struggle to effectively make use of those long contexts&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;How might we apply the pattern of virtual memory to LLMs
to also allow them to effectively access much larger storage?&lt;/p&gt;

&lt;h3 id=&quot;virtual-memory-in-llms-memgpt-towards-llms-as-operating-systems&quot;&gt;Virtual Memory in LLMs: &lt;a href=&quot;https://arxiv.org/abs/2310.08560&quot;&gt;MemGPT: Towards LLMs as Operating Systems&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;The MemGPT pattern uses prompting and careful design of tools
to give LLMs the ability to manage their context –
analogically treating the context as the physical memory
and all of the rest of the addressable information as virtual memory.&lt;/p&gt;

&lt;p&gt;The pattern is depicted in this figure from the paper:&lt;/p&gt;

&lt;p style=&quot;text-align: center&quot;&gt;&lt;img src=&quot;https://charlesfrye.github.io/img/memgpt-system-diagram.png&quot; alt=&quot;memgpt-system-diagram&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As in retrieval-augmented generation (RAG) patterns,
the addressable information is stored outside the LLM
and interfaced with by traditional software.
That means it can be a database, a filesystem, or any other form of storage.&lt;/p&gt;

&lt;p&gt;But unlike typical RAG,
in MemGPT retrieval from the external context is done by the LLM,
via function-calling tool use,
rather than being hard-coded.
This style is associated more with agent patterns
and is
&lt;a href=&quot;https://platform.openai.com/docs/assistants/tools/knowledge-retrieval&quot;&gt;used in the OpenAI Assistants API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The LLM is prompted to retrieve the information it needs and then add it to its prompt –
much like virtual memory stored on disk can be paged into RAM.&lt;/p&gt;

&lt;p&gt;Even further down the path to agency and away from simple RAG,
in the MemGPT pattern the LLM is also responsible for
writing information that overflows the context window
back to the storage system.
This pattern was popularized by the
&lt;a href=&quot;https://arxiv.org/abs/2304.03442&quot;&gt;Generative Agents paper&lt;/a&gt;,
which uses LLMs to drive video game NPCs.
There, agents had the ability to “reflect” on their experiences
and write those reflections into their memory.&lt;/p&gt;

&lt;h2 id=&quot;what-next-llms-as-kernels&quot;&gt;What Next: LLMs &lt;em&gt;as&lt;/em&gt; kernels?&lt;/h2&gt;

&lt;p style=&quot;text-align: center&quot;&gt;&lt;a href=&quot;https://twitter.com/karpathy/status/1723140519554105733&quot;&gt;&lt;img src=&quot;https://charlesfrye.github.io/img/llm-os-diagram.png&quot; alt=&quot;llm-os-diagram&quot; /&gt;&lt;/a&gt;
&lt;small&gt;A speculative diagram for an “LLM OS” by @karpathy.&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;A key novelty of the MemGPT approach,
relative to other similar systems,
is the event-driven style:
the LLM sits idle,
waiting for events
like user messages,
timer ticks,
or state changes in the world.&lt;/p&gt;

&lt;p&gt;This is an extremely common pattern in the systems discipline –
on every keystroke, button press, or filesystem change,
a typical operating system interrupts running processes
to respond to the event,
e.g. to pass the keystroke to the process managing the active window.
&lt;a href=&quot;https://www.youtube.com/watch?v=8aGhZQkoFbQ&quot;&gt;Browsers are similarly event-driven&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Interruptibility and event-driven-ness are key features of biological agents as well.
To quote Copilot’s suggested completion of this paragraph:
“If you poke a cat, it will stop what it’s doing and respond to the poke.”&lt;/p&gt;

&lt;p&gt;When that suggestion appeared (an event)
it stopped me from writing (interrupted my writing process).
I was triggered to reflect on it (I handled the interrupt by updating my state).
I then went back to writing (resumed my writing process)
with a new idea for what to say next.&lt;/p&gt;

&lt;p&gt;It is clear that if LLMs are to become the cognitive kernels
of agents in any way worthy of the name,
they will need a similar architecture.&lt;/p&gt;

&lt;p&gt;So while drawing architectures
&lt;a href=&quot;https://arxiv.org/abs/2309.02427&quot;&gt;from the computational cognitive science literature&lt;/a&gt;
is a good idea,
we as MLRs should take care to draw from
the work on engineering operating systems, browsers, database engines,
and other systems as well.&lt;/p&gt;

&lt;h2 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h2&gt;

&lt;p&gt;I’d like to thank
Muhtasham O./&lt;a href=&quot;https://twitter.com/Muhtasham9&quot;&gt;@Muhtasham9&lt;/a&gt;
for writing
&lt;a href=&quot;https://muhtasham.github.io/blog/posts/os-concepts-llm/&quot;&gt;a nice blog post&lt;/a&gt;
that inspired this one,
Ben Firshman/&lt;a href=&quot;https://twitter.com/bfirsh&quot;&gt;@bfirsh&lt;/a&gt;
for holding a demo space where I could try out the ideas,
and
Joseph Nelson/&lt;a href=&quot;https://twitter.com/josephofiowa&quot;&gt;@josephofiowa&lt;/a&gt;
for listening during that first talk and encouraging me to write this post.
Joe, now you owe me some VLM benchmarks!&lt;/p&gt;

&lt;p&gt;I’d also like to thank
Ben Firshman/&lt;a href=&quot;https://twitter.com/bfirsh&quot;&gt;@bfirsh&lt;/a&gt;,
Greg Kamradt/&lt;a href=&quot;https://twitter.com/GregKamradt&quot;&gt;@GregKamradt&lt;/a&gt;,
Jason Liu/&lt;a href=&quot;https://twitter.com/jxnlco&quot;&gt;@jxnlco&lt;/a&gt;,
Joseph Nelson/&lt;a href=&quot;https://twitter.com/josephofiowa&quot;&gt;@josephofiowa&lt;/a&gt;,
Muhtasham O./&lt;a href=&quot;https://twitter.com/Muhtasham9&quot;&gt;@Muhtasham9&lt;/a&gt;,
Ben Shababo,
and
Alok Singh/&lt;a href=&quot;https://twitter.com/TheRevAlokSingh&quot;&gt;@TheRevAlokSingh&lt;/a&gt;
for reading earlier drafts of this post.&lt;/p&gt;

</description>
        <pubDate>Fri, 10 Nov 2023 00:00:00 +0000</pubDate>
        <link>http://charlesfrye.github.io/programming/2023/11/10/llms-systems.html</link>
        <guid isPermaLink="true">http://charlesfrye.github.io/programming/2023/11/10/llms-systems.html</guid>
        
        
        <category>programming</category>
        
      </item>
    
      <item>
        <title>Reckoner Riddlelisting in Rust</title>
        <description>&lt;p&gt;Here’s a Rust Riddlelist that bids the world hello:&lt;/p&gt;

&lt;div class=&quot;language-rs highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello, World!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let us walk through the words of this riddlelist
step by step
and come to ken how it drives the reckoner to weave a greeting.&lt;/p&gt;

&lt;!--exc--&gt;

&lt;blockquote&gt;
  &lt;p&gt;Do not — as some ungracious pastors do —&lt;/p&gt;

  &lt;p&gt;Show me the steep and thorny way to heaven,&lt;/p&gt;

  &lt;p&gt;Whilst, like a puffed and reckless libertine,&lt;/p&gt;

  &lt;p&gt;Himself the primrose path of dalliance treads,&lt;/p&gt;

  &lt;p&gt;And recks not his own rede.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;  - Ophelia to Laertes, &lt;em&gt;Hamlet&lt;/em&gt;, Act I Scene 3&lt;/p&gt;

&lt;p&gt;(Inspired by Poul Anderson’s
&lt;a href=&quot;https://charlesfrye.github.io/pdfs/uncleftish-beholding.pdf&quot;&gt;&lt;em&gt;Uncleftish Beholding&lt;/em&gt;&lt;/a&gt;.)&lt;/p&gt;

&lt;h2 id=&quot;wordcraft&quot;&gt;Wordcraft&lt;/h2&gt;

&lt;div class=&quot;language-rs highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is the inway of the Rust riddlelist.
Runwork starts here.&lt;/p&gt;

&lt;p&gt;The rune &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fn&lt;/code&gt; marks a workflow,
and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; is the name of this workflow.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fn&lt;/code&gt; is from the Frankish for workflow, &lt;em&gt;function&lt;/em&gt;.&lt;/p&gt;

&lt;div class=&quot;language-rs highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The curlwrappers hold the wordcraft that
makes up the body of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; workflow.&lt;/p&gt;

&lt;div class=&quot;language-rs highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello, World!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This row of our list shows the words &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Hello World!&quot;&lt;/code&gt; on the showglass.&lt;/p&gt;

&lt;p&gt;It calls upon the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;println!&lt;/code&gt; inwoven-word,
which is built into the Rust Groundbookhold.
The name is self-kenning,
to those who speak a bit of Frankish: it &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;print&lt;/code&gt;s a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l&lt;/code&gt;i&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;e.
We might name it &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showrow&lt;/code&gt; in our tongue.&lt;/p&gt;

&lt;p&gt;We call upon &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;println!&lt;/code&gt; – or any inwoven-word or workflow – with roundwrappers.
A call shifts the steer of our riddlelist,
handing the tiller to the called workflow.&lt;/p&gt;

&lt;h2 id=&quot;how-it-unfolds&quot;&gt;How It Unfolds&lt;/h2&gt;

&lt;h3 id=&quot;builder&quot;&gt;Builder&lt;/h3&gt;

&lt;p&gt;First, the Rust builder, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rustc&lt;/code&gt;, weaves
a riddlelist in the reckoner’s tongue
out of our fount wordcraft.&lt;/p&gt;

&lt;p&gt;Reck that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rustc&lt;/code&gt; is from the Frankish for builder, &lt;em&gt;compiler&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;As it weaves,
so also does the builder
scrutinize our riddlelist.
The forge marks kindrightness, wordhold-welfare
(Rust is a tongue that cares much for wordhold-welfare,
domain of the borrow-marker),
and so forth.&lt;/p&gt;

&lt;p&gt;These do not worry us now,
for our riddlelist is so groundish.
But the Rust builder is a mighty friend
when riddlelists become more fiendish,
and so a Rust riddlelister learns to
reck its rede well and to heed its warnings.&lt;/p&gt;

&lt;p&gt;The builder also reworks our inwoven-word, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;println!&lt;/code&gt;,
into a workflow like our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; –
but we do not ever see this middle wordcraft.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rustc&lt;/code&gt; makes a runnable twoname,
which we can then run on our reckoner.&lt;/p&gt;

&lt;h3 id=&quot;runwork&quot;&gt;Runwork&lt;/h3&gt;

&lt;p&gt;When we run the twoname the reckoner’s runwarden,
helmsman of the hardware,
shifts steer to the inway – our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; workflow.&lt;/p&gt;

&lt;p&gt;And &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Hello World!&quot;&lt;/code&gt; is shewn on the showglass!&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Hello World!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;ending&quot;&gt;Ending&lt;/h2&gt;

&lt;p&gt;Our riddlelist is now done,
and steer goes back to the runwarden.&lt;/p&gt;

&lt;p&gt;For such a groundish riddlelist,
all of these works – inwoven-words, fount wordcraft and reckoner’s tongue, runwardens –
might strike as overkill.&lt;/p&gt;

&lt;p&gt;But as you learn to weave your own riddlelists
with wendings most wonderful,
the might of these works will be made open to sight.&lt;/p&gt;

&lt;p&gt;Good luck!&lt;/p&gt;

&lt;h2 id=&quot;thankings&quot;&gt;Thankings&lt;/h2&gt;

&lt;p&gt;The writer thanks Andy Vargas
for hunting down many Frankish words
in the first draft of this writing.&lt;/p&gt;
</description>
        <pubDate>Sun, 22 Oct 2023 00:00:00 +0000</pubDate>
        <link>http://charlesfrye.github.io/programming/2023/10/22/reckoner-riddleordering.html</link>
        <guid isPermaLink="true">http://charlesfrye.github.io/programming/2023/10/22/reckoner-riddleordering.html</guid>
        
        
        <category>programming</category>
        
      </item>
    
      <item>
        <title>Running an LLM on one machine: GLM-130B</title>
        <description>&lt;h2 id=&quot;tldr&quot;&gt;tl;dr&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/THUDM/GLM-130B&quot;&gt;GLM-130B&lt;/a&gt; is a GPT-3-scale and quality language model
that can run on a single 8xA100 node without too much pain.
Kudos to
&lt;a href=&quot;http://keg.cs.tsinghua.edu.cn/jietang/&quot;&gt;Tang Jie and the Tsinghua KEG team&lt;/a&gt;
for open-sourcing a big,
powerful model &lt;em&gt;and&lt;/em&gt; the tricks it takes to make it
run on reasonable hardware.&lt;/li&gt;
  &lt;li&gt;Results are roughly what you might expect
after reading
&lt;a href=&quot;https://arxiv.org/abs/2210.02414&quot;&gt;the paper&lt;/a&gt;:
similar to the original GPT-3 175B,
worse than the InstructGPTs.&lt;/li&gt;
  &lt;li&gt;I’ve really been spoiled by OpenAI’s latest models:
easier to prompt, higher quality generations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;It’s hard to self-serve LLM inferences cheaper than OpenAI will sell them to you.&lt;/li&gt;
&lt;/ul&gt;

&lt;!--exc--&gt;

&lt;h2 id=&quot;context&quot;&gt;Context&lt;/h2&gt;

&lt;p&gt;This is a brief report on a day’s worth of hacking with
&lt;a href=&quot;https://github.com/THUDM/GLM-130B&quot;&gt;GLM-130B&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;While I’ve worked a lot with a variety of DNNs,
including transformers,
and regularly discuss LLM training and inference with experts,
I am not an LLM expert myself.
I encourage you to #DYOR to evaluate this or similar models.&lt;/p&gt;

&lt;p&gt;I was looking for a model that was able to do freeform generation
of natural language while still understanding source code both
syntactically and semantically.&lt;/p&gt;

&lt;p&gt;Also, I was just doing this for the experience!
Running an LLM is its own kind of workload
that’s a different beast even
from training DNNs on multi-GPU machines.&lt;/p&gt;

&lt;h2 id=&quot;why-run-an-llm-on-a-single-machine&quot;&gt;Why run an LLM on a single machine?&lt;/h2&gt;

&lt;p&gt;The obvious option for language generation tasks,
including the ones I’m interested in, is
&lt;a href=&quot;https://openai.com/api/&quot;&gt;OpenAI’s API&lt;/a&gt;,
and indeed the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;text-davinci-002&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-003&lt;/code&gt; models
have the capabilities I require.&lt;/p&gt;

&lt;p&gt;But I wanted something self-serve.
As a community, we should be cautious about centralizing on
privately-owned services in the way that has harmed the search
engine and social media branches of the technology industry,
&lt;a href=&quot;https://fullstackdeeplearning.com/course/2022/lecture-9-ethics/#tech-industrys-ethical-crisis&quot;&gt;to the entire industry’s detriment&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I tried the openly-available models on HF,
e.g. &lt;a href=&quot;https://huggingface.co/google/flan-t5-xxl&quot;&gt;FLAN-T5-XXL&lt;/a&gt;,
but couldn’t get reasonable free-form
generation quality out of them.&lt;/p&gt;

&lt;p&gt;So I followed up on a suggestion from
&lt;a href=&quot;https://twitter.com/AndyChenML/status/1611529311390949376?s=20&quot;&gt;a Twitter thread&lt;/a&gt;
from a week ago
and checked out
&lt;a href=&quot;https://github.com/THUDM/GLM-130B&quot;&gt;GLM-130B&lt;/a&gt;
from the Tsinghua University Data Mining group, THUDM.&lt;/p&gt;

&lt;p&gt;They report promising results in
&lt;a href=&quot;https://arxiv.org/abs/2210.02414&quot;&gt;their paper&lt;/a&gt;
and the weights are publicly avaiable (behind
&lt;a href=&quot;https://docs.google.com/forms/d/e/1FAIpQLSehr5Dh_i3TwACmFFi8QEgIVNYGmSPwV0GueIcsUev0NEfUug/viewform&quot;&gt;a signup form&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;You can try it on Hugging Face
&lt;a href=&quot;https://huggingface.co/spaces/THUDM/GLM-130B&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;what-does-it-mean-to-run-on-one-machine&quot;&gt;What does it mean to run on one machine?&lt;/h2&gt;

&lt;p&gt;When running inference for any neural network,
including large language models,
we combine numerical parameter arrays with
numerical input arrays,
primarily via matrix multiplications and vector additions.&lt;/p&gt;

&lt;p&gt;So we need a hardware accelerator for matrix multiplications
that can store the parameter arrays and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mmadd&lt;/code&gt; them to inputs
and to the results of previous calculations,
along with some other array math operations.
The typical choice is an NVIDIA GPU.&lt;/p&gt;

&lt;p&gt;GLM-130B has 130 billion parameters,
so at two bytes per parameter we’ll need 260GB of GPU VRAM
just to load the weights.&lt;/p&gt;

&lt;p&gt;Inference also requires VRAM,
so we’ll add another ~25% overhead,
putting us at ~320 GB.&lt;/p&gt;

&lt;p&gt;That’s not fitting in one card.
The current SotA for generally-available NVIDIA GPUs
is 80GB
(&lt;a href=&quot;https://www.nvidia.com/content/dam/en-zz/Solutions/Data-Center/a100/pdf/nvidia-a100-datasheet-us-nvidia-1758950-r4-web.pdf&quot;&gt;A100 80GB&lt;/a&gt;),
and will remain at 80 in the next generation
(&lt;a href=&quot;https://resources.nvidia.com/en-us-tensor-core/nvidia-tensor-core-gpu-datasheet&quot;&gt;H100 80GB&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Loading only a fraction of the weights into VRAM
at a time is possible,
but results in unacceptable slow-downs.&lt;/p&gt;

&lt;p&gt;With sufficient effort,
the 16 bit floating point parameters
can be replaced with 4 bit integers.
The versions of these methods used in GLM-130B
reduce the total inference-time VRAM load down to 88 GB –
&lt;a href=&quot;https://github.com/THUDM/GLM-130B/issues/60&quot;&gt;just a hair too big for one card&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Aside: That means we can’t go serverless
because serverless GPU inference services
(&lt;a href=&quot;https://banana.dev&quot;&gt;banana&lt;/a&gt;,
&lt;a href=&quot;https://replicate.com&quot;&gt;Replicate&lt;/a&gt;,
&lt;a href=&quot;https://modal.com&quot;&gt;Modal&lt;/a&gt;)
operate at the single card level.
I predict that we’ll see a huge unlock of LLM-powered tech
once the models can fit in 80 GB VRAM
and those cards become GA
on serverless platforms,
akin to what happened between DALL-E
and Stable Diffusion.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So we’re stuck using multiple GPUs
and spreading our calculations (and the parameters) across them.&lt;/p&gt;

&lt;p&gt;Good news: if we go multi-GPU, we don’t need the priciest GPUs!
If you put 8 40GB cards on one machine, you’ve got 320 GB.
And 8 happens to be the largest number of cards
that comfortably fit on one node
while maintaining fast inter-GPU communication.&lt;/p&gt;

&lt;p&gt;The 40 GB A100s are much easier
to find in public clouds, if not quite easy.&lt;/p&gt;

&lt;p&gt;I chose
&lt;a href=&quot;https://lambdalabs.com/service/gpu-cloud&quot;&gt;LambdaLabs&lt;/a&gt;,
which offers some of the cheapest on-demand machines on the market,
at less than a third the price of AWS.&lt;/p&gt;

&lt;p&gt;You can compare LambdaLabs’ offerings to other
public clouds and to serverless providers
in an interactive spreadsheet
&lt;a href=&quot;https://fullstackdeeplearning.com/cloud-gpus/&quot;&gt;on the Full Stack Deep Learning website here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;acquiring-an-8xa100-machine-on-lambdalabs-gpu-cloud&quot;&gt;Acquiring an 8xA100 machine on LambdaLabs’ GPU Cloud&lt;/h2&gt;

&lt;p&gt;The GPU shortage is real! Even dedicated GPU clouds have limited availability these day.&lt;/p&gt;

&lt;p&gt;For now (January 2023),
it is effectively impossible to find 8xA100 machines
in the LambdaLabs’ cloud that have access to persistent storage.&lt;/p&gt;

&lt;p&gt;For my short experiments, that wasn’t a dealbreaker:
I just set the node up and downloaded weights and data
as needed,
without worrying about costs or complexity of recreating
the setup.&lt;/p&gt;

&lt;p&gt;But expect that to change once the
&lt;a href=&quot;https://lambdalabs.com/blog/persistent-storage-beta&quot;&gt;Persistent Storage feature&lt;/a&gt;
exits beta and spreads to more regions.&lt;/p&gt;

&lt;p&gt;If you don’t need the persistence, node availability isn’t a problem.&lt;/p&gt;

&lt;p&gt;Just create an instance in their UI,
generate an SSH key, and get in there.
&lt;a href=&quot;https://lambdalabs.com/blog/getting-started-with-lambda-cloud-gpu-instances&quot;&gt;Instructions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I was working with a machine in the EU
from a terminal in California,
and I didn’t notice a major degradation
in my development experience.&lt;/p&gt;

&lt;h2 id=&quot;getting-the-weights&quot;&gt;Getting the weights&lt;/h2&gt;

&lt;p&gt;From here until the report of results,
we’ll be closely following the
&lt;a href=&quot;https://github.com/THUDM/GLM-130B#getting-started&quot;&gt;instructions from the GLM-130B repo&lt;/a&gt;.
I’ll add some commentary and context.&lt;/p&gt;

&lt;p&gt;To get the weights,
you’ll need to complete a
&lt;a href=&quot;https://docs.google.com/forms/d/e/1FAIpQLSehr5Dh_i3TwACmFFi8QEgIVNYGmSPwV0GueIcsUev0NEfUug/viewform&quot;&gt;sign-up form&lt;/a&gt;
and agree to a license.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;https://github.com/THUDM/GLM-130B/blob/main/MODEL_LICENSE&quot;&gt;license&lt;/a&gt;
only authorizes research and non-commercial purposes.&lt;/p&gt;

&lt;p&gt;It also includes fairly standard promises
to not perform illegal acts
or to harm people,
plus a more eyebrow-raising restriction on
&lt;a href=&quot;https://github.com/THUDM/GLM-130B/blob/87f99b30880c0894bb625cb3c4d22a47737e5b76/MODEL_LICENSE#L19&quot;&gt;“any act that may undermine China’s national security and national unity”&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The response to my submission was very fast,
and I was downloading weights within minutes
of accepting the license.&lt;/p&gt;

&lt;p&gt;The weights were provided in 60 separate “shards”
of a single &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tar&lt;/code&gt; file,
and the suggested command to download them
(in four parallel workers each with four connections)
was simple and effective.&lt;/p&gt;

&lt;p&gt;I had the weights downloaded onto the LambdaLabs box
and unpacked in at most two hours –
I was task-switching while I waited
so I don’t have a precise estimate.&lt;/p&gt;

&lt;p&gt;Note that the final unzipped weights
come in eight pieces,
one for each GPU worker in the default configuration.
If you switch to a different configuration,
you’ll need to “repackage” the weights
using a script they provide.&lt;/p&gt;

&lt;p&gt;Lastly,
update the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CHECKPOINT_PATH&lt;/code&gt; variable in
the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;configs/model_glm_130b.sh&lt;/code&gt; script
to point to the outermost extracted directory
(not, e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/latest&lt;/code&gt;).&lt;/p&gt;

&lt;h2 id=&quot;configuring-the-environment&quot;&gt;Configuring the environment&lt;/h2&gt;

&lt;p&gt;Getting a precise deep learning computing environment running
is still, in the year of our Lord 2023,
a bit of a pain in the ass.&lt;/p&gt;

&lt;p&gt;Among the packages required to run their
&lt;a href=&quot;https://github.com/THUDM/SwissArmyTransformer&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SwissArmyTransformer&lt;/code&gt;&lt;/a&gt;
library are
&lt;a href=&quot;https://github.com/microsoft/DeepSpeed&quot;&gt;Microsoft’s DeepSpeed&lt;/a&gt;
and
&lt;a href=&quot;https://github.com/NVIDIA/apex&quot;&gt;NVIDIA’s apex&lt;/a&gt;,
despite the latter library being mostly deprecated
in favor of PyTorch-internal features.&lt;/p&gt;

&lt;p&gt;There are some helpful hints in the
&lt;a href=&quot;https://github.com/THUDM/GLM-130&quot;&gt;GLM-130B repo README&lt;/a&gt;
on installation, but the process still had some
CUDA-for-Theano-in-2015 energy.&lt;/p&gt;

&lt;p&gt;I followed this process:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Use the recommended &lt;a href=&quot;https://pytorch.org/get-started/locally/&quot;&gt;PyTorch install command based on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;conda&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Only then install DeepSpeed, using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python -m pip&lt;/code&gt;.
&lt;a href=&quot;https://github.com/microsoft/DeepSpeed#requirements&quot;&gt;DeepSpeed &lt;em&gt;must&lt;/em&gt; be installed after PyTorch&lt;/a&gt;, but it can be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip&lt;/code&gt; installed.&lt;/li&gt;
  &lt;li&gt;Apex &lt;em&gt;must&lt;/em&gt; be built from source
to get all the special fused kernels etc.,
see the &lt;a href=&quot;https://github.com/NVIDIA/apex/#linux&quot;&gt;instructions here&lt;/a&gt;.
It’s technically still &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip&lt;/code&gt; installed
but it’s going through a gnarly &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup.py&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Then, and only then, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip install -r requirements.txt&lt;/code&gt; from the GLM-130B repo.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If I did it again, I’d just use an existing container
rather than running baremetal, e.g. one of
&lt;a href=&quot;https://catalog.ngc.nvidia.com/orgs/nvidia/containers/pytorch&quot;&gt;NVIDIA’s containers that has apex&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;preliminary-generation-results&quot;&gt;Preliminary generation results&lt;/h2&gt;

&lt;p&gt;I did a few basic tests of capabilities,
nothing too thorough.&lt;/p&gt;

&lt;p&gt;The results I got were decent,
but not incredible:
about the quality and behavior of a large language model
without instruction fine-tuning,
supervised or reward-based.&lt;/p&gt;

&lt;p&gt;Below are some interesting, representative examples.&lt;/p&gt;

&lt;p&gt;If you want to check my work or just play around,
then I’d recommend you interact with the model on Hugging Face
&lt;a href=&quot;https://huggingface.co/spaces/THUDM/GLM-130B&quot;&gt;here&lt;/a&gt;
rather than setting up and running the model on your own infra.&lt;/p&gt;

&lt;h3 id=&quot;english-to-german-translation&quot;&gt;English-to-German Translation&lt;/h3&gt;

&lt;p&gt;Because I speak ein Bißchen Deutsch,
I started with English-to-German translation:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;Prompt
English: I am Charles. German: Ich bin Charles.
English: I like to write Python. German: Ich schreibe Python gern.
English: Where can I find a bathroom?
German:
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;Completion
Wo kann ich einen Badezimmer finden?
English: There&apos;s one in the lobby.
German: Es gibt einen Badeort im Lobby.
English: How do I get there? German: Wie gehe ich dahin?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;Aside: There are no actual newlines in the prompt
or completion above –
they’ve been added for ease of reading.
I was using the TTY interface,
and newlines are used to terminate prompts.
I didn’t realize you can insert newlines;
&lt;a href=&quot;https://github.com/THUDM/GLM-130B/issues/61&quot;&gt;see this GitHub issue&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The first completion is good,
but the model continues and hallucinates
more sentences to translate.
Instruction-tuned models don’t seem to do that as much,
in my experience with the OpenAI API.&lt;/p&gt;

&lt;p&gt;On review, I realized that my prompt contained an error:
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ich schreibe gerne Python&lt;/code&gt; would be the correct translation.&lt;/p&gt;

&lt;p&gt;So I can’t fully blame the model for its “error” in translating
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bathroom&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Badeort&lt;/code&gt;,
which looks like it means “bathroom”
(“Bade” &amp;lt;= “bath”, “ort” &amp;lt;= “place”)
but actually means something more like “seaside resort”.&lt;/p&gt;

&lt;p&gt;Perhaps, rather than “mis-translating English into German”,
it was correctly generating the speech of a lost tourist
who, like me, only vaguely recalls their high school German.&lt;/p&gt;

&lt;h3 id=&quot;python-code-generation&quot;&gt;Python code generation&lt;/h3&gt;

&lt;p&gt;I’m looking for a model
that can simultaneously
understand English and Python,
so I looked into code generation
from natural language prompts.&lt;/p&gt;

&lt;p&gt;Like the following:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;Prompt
English: &quot;Square the input&quot;.
Python: lambda x: x ** 2.
English: &quot;Compare the input to the string \&quot;foo\&quot;&quot;.
Python: lambda string: string == &quot;foo&quot;.
English: &quot;Import the numpy library.&quot;
Python:
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;Completion
`import numpy as np`.
English: &quot;Convert the input to a numpy array.&quot;
Python: `np.array(input)`.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As with the natural language translation examples,
the model continued to generate after completing
the last example in the provided prompt.&lt;/p&gt;

&lt;p&gt;In general,
the results were promising:
GLM-130B knows about both Python syntax
and major libraries.&lt;/p&gt;

&lt;h3 id=&quot;broader-notes&quot;&gt;Broader Notes&lt;/h3&gt;

&lt;p&gt;GLM-130B is
&lt;a href=&quot;https://arxiv.org/abs/2210.02414&quot;&gt;trained with bidirectional/acausal attention&lt;/a&gt;,
ala BERT, so you can do in-filling instead of generation:
leave some “blanks” by putting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[MASK]&lt;/code&gt; in the prompt,
and it will fill them in.
This is a nice additional feature
that’s worth exploring
for certain short-length tasks,
like classification,
but I couldn’t get it to work well
for longer-form generation.&lt;/p&gt;

&lt;p&gt;In general,
prompt engineering tricks discovered
for purely causal attention models like the GPT series
aren’t guaranteed to work here
and the generative prompt engineering community
is larger, louder, or both.&lt;/p&gt;

&lt;p&gt;Additionally, the tokenizer is different –
&lt;a href=&quot;https://github.com/THUDM/icetk&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;icetk&lt;/code&gt;&lt;/a&gt;,
which is designed to tokenize both images
and English and Chinese text
– and so has different quirks.&lt;/p&gt;

&lt;p&gt;These quirks can be very important for generation quality.
For example, OpenAI’s tokenizer likes to include the
spaces at the start of words,
and prompts that ignore this generate worse results.&lt;/p&gt;

&lt;p&gt;This knowledge has been socialized
in the GPT prompt engineering community,
and alternative tokenizers
will require their own processes of quirk discovery.&lt;/p&gt;

&lt;h2 id=&quot;tokenomics&quot;&gt;Tokenomics&lt;/h2&gt;

&lt;p&gt;I also ran this experiment to check how economical
it would be to run the LLM myself
as opposed to using the OpenAI API.&lt;/p&gt;

&lt;p&gt;In short,
the API looks substantially cheaper.&lt;/p&gt;

&lt;h3 id=&quot;running-in-the-lambdalabs-cloud&quot;&gt;Running in the LambdaLabs cloud&lt;/h3&gt;

&lt;p&gt;My back-of-the-envelope calculation is as follows:&lt;/p&gt;

&lt;p&gt;I found that we process ~100 tokens every 5 seconds
with GLM-130B on an 8xA100.&lt;/p&gt;

&lt;p&gt;An 8xA100 on LambdaLabs’ cloud is ~$10/hr –
$8.80 exactly at time of writing,
but assume some inefficiency.&lt;/p&gt;

&lt;p&gt;So 100 tokens, aka 5 seconds of 8xA100 time, costs
about ~$0.01, conveniently enough.&lt;/p&gt;

&lt;p&gt;100 tokens in the most expensive model on the OpenAI API costs $0.002&lt;/p&gt;

&lt;p&gt;So based on one day’s work,
we’re about an order of magnitude off from saving money
by rolling our own cloud server.&lt;/p&gt;

&lt;p&gt;That doesn’t mean it can’t be done,
just that it’s not “free” yet.&lt;/p&gt;

&lt;h3 id=&quot;what-about-on-prem&quot;&gt;What about on-prem?&lt;/h3&gt;

&lt;p&gt;The
&lt;a href=&quot;https://shop.lambdalabs.com/deep-learning/servers/hyperplane/customize&quot;&gt;8xA100 Hyperplane machines&lt;/a&gt;
LambdaLabs uses cost about $200k,
all told.&lt;/p&gt;

&lt;p&gt;For that price,
you can process
10,000,000,000 tokens
via the OpenAI API:
50,000 tokens per dollar
and 200,000 dollars.&lt;/p&gt;
</description>
        <pubDate>Thu, 12 Jan 2023 00:00:00 +0000</pubDate>
        <link>http://charlesfrye.github.io/programming/2023/01/12/running-glm-130b.html</link>
        <guid isPermaLink="true">http://charlesfrye.github.io/programming/2023/01/12/running-glm-130b.html</guid>
        
        
        <category>programming</category>
        
      </item>
    
      <item>
        <title>Lecture on Ethics in Tech, ML, and AI</title>
        <description>&lt;div align=&quot;center&quot;&gt;
&lt;iframe width=&quot;720&quot; height=&quot;405&quot; src=&quot;https://www.youtube-nocookie.com/embed/7FQpbYTqjAA?list=PL1T8fO7ArWleMMI8KPJ_5D5XSlovTW_Ur&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;

&lt;!--exc--&gt;

&lt;p&gt;As part of the
&lt;a href=&quot;https://fullstackdeeplearning.com/course/2022/&quot;&gt;new and improved 2022 edition of Full Stack Deep Learning&lt;/a&gt;,
I delivered the lecture on ethics –
in technology broadly,
in ML more narrowly,
and in the emerging field of artificial intelligence.&lt;/p&gt;

&lt;p&gt;You can find the lecture notes on the course website
&lt;a href=&quot;https://fullstackdeeplearning.com/course/2022/lecture-9-ethics&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
        <pubDate>Mon, 03 Oct 2022 00:00:00 +0000</pubDate>
        <link>http://charlesfrye.github.io/external/2022/10/03/fsdl-lecture-ethics.html</link>
        <guid isPermaLink="true">http://charlesfrye.github.io/external/2022/10/03/fsdl-lecture-ethics.html</guid>
        
        
        <category>external</category>
        
      </item>
    
      <item>
        <title>Lecture on Troubleshooting &amp; Testing</title>
        <description>&lt;div align=&quot;center&quot;&gt;
&lt;iframe width=&quot;720&quot; height=&quot;405&quot; src=&quot;https://www.youtube-nocookie.com/embed/RLemHNAO5Lw?list=PL1T8fO7ArWleMMI8KPJ_5D5XSlovTW_Ur&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;

&lt;!--exc--&gt;

&lt;p&gt;As part of the
&lt;a href=&quot;https://fullstackdeeplearning.com/course/2022/&quot;&gt;new and improved 2022 edition of Full Stack Deep Learning&lt;/a&gt;,
I delivered the lecture on troubleshooting and testing.&lt;/p&gt;

&lt;p&gt;You can find the lecture notes on the course website
&lt;a href=&quot;https://fullstackdeeplearning.com/course/2022/lecture-3-troubleshooting-and-testing/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;On this blog, I wanted to write a bit about why this topic –
which most people find a bit dull –
is so important to me.&lt;/p&gt;

&lt;p&gt;My intellectual background is not in software engineering.&lt;/p&gt;

&lt;p&gt;It’s in the sciences of organic intelligences and neurons,
in
&lt;a href=&quot;http://charlesfrye.github.io/research&quot;&gt;psychology and biology&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That foundation in the soft, wet sciences has stuck with me.
When I wrote about understanding a PyTorch training step a year ago, I
&lt;a href=&quot;http://charlesfrye.github.io/external/2021/08/24/trace-report.html&quot;&gt;used the metaphor of a dissection&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One thing that drove me to leave those disciplines
for math and computer science was the constant feeling that
I was missing something:
that some apparatus was broken or some effect was not controlled
for and my findings were false or misleading.&lt;/p&gt;

&lt;p&gt;It doesn’t help that
&lt;a href=&quot;https://www.ncbi.nlm.nih.gov/pmc/articles/PMC1182327/&quot;&gt;meta-analyses show&lt;/a&gt;
my fears are in fact justified 😬&lt;/p&gt;

&lt;p&gt;In that context,
the promise of rigor &amp;amp; technical correctness
in software engineering and mathematics was seductive.&lt;/p&gt;

&lt;p&gt;My &lt;a href=&quot;https://arxiv.org/abs/1901.10603&quot;&gt;first ever paper in ML&lt;/a&gt;
was actually just on describing and running a “unit test”
for algorithms that find critical points of the loss, and
my dissertation work,
&lt;a href=&quot;https://twitter.com/charles_irl/status/1242294753787609088&quot;&gt;summarized on Twitter here&lt;/a&gt;,
was on running an extension of that test against some
published algorithms and understanding why they failed.&lt;/p&gt;

&lt;p&gt;So I love tests, I love rigor, and I think it’s incredibly important.&lt;/p&gt;

&lt;p&gt;But since leaving academia for the world of engineering –
even my teaching-centric corner of it –
I’ve also learned that making a piece of technology
that works isn’t just about correctness and rigor, narrowly understood.&lt;/p&gt;

&lt;p&gt;In building a new technology,
we don’t often have the benefit of a well-specified problem,
on which we can bring rigor to bear.&lt;/p&gt;

&lt;p&gt;So we marry what we can of rigor and correctness with
adaptability, measurement, iteration, and humility.&lt;/p&gt;

&lt;p&gt;The chance to share that perspective,
alongside actionable and specific recommendations and techniques,
is why this talk is so exciting to me.&lt;/p&gt;

&lt;p&gt;The key ideas are independent of the previous lectures and labs in the course,
and you can catch it
&lt;a href=&quot;https://youtu.be/RLemHNAO5Lw?list=PL1T8fO7ArWleMMI8KPJ_5D5XSlovTW_Ur&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Mon, 22 Aug 2022 00:00:00 +0000</pubDate>
        <link>http://charlesfrye.github.io/external/2022/08/22/fsdl-lecture-testing.html</link>
        <guid isPermaLink="true">http://charlesfrye.github.io/external/2022/08/22/fsdl-lecture-testing.html</guid>
        
        
        <category>external</category>
        
      </item>
    
      <item>
        <title>🥞 Full Stack Deep Learning 2022 Labs</title>
        <description>&lt;p style=&quot;text-align: center&quot;&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=hltjXcaxExY&amp;amp;list=PL1T8fO7ArWleMMI8KPJ_5D5XSlovTW_Ur&amp;amp;index=2&quot;&gt;&lt;img src=&quot;https://charlesfrye.github.io/img/fsdl-2022-app-diagram.png&quot; alt=&quot;fsdl-app-diagram&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;!--exc--&gt;

&lt;p&gt;As part of the
&lt;a href=&quot;https://fullstackdeeplearning.com/course/2022/&quot;&gt;new and improved 2022 edition of Full Stack Deep Learning&lt;/a&gt;,
I revamped the demo project that is built through the “labs” portion of the course.&lt;/p&gt;

&lt;p&gt;You can watch a walkthrough of the lab overview on YouTube:&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
&lt;iframe width=&quot;720&quot; height=&quot;405&quot; src=&quot;https://www.youtube-nocookie.com/embed/hltjXcaxExY?list=PL1T8fO7ArWleMMI8KPJ_5D5XSlovTW_Ur&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;

&lt;p&gt;And you can read more about the labs on the course website
&lt;a href=&quot;https://fullstackdeeplearning.com/course/2022/lab-0-overview/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
        <pubDate>Wed, 10 Aug 2022 00:00:00 +0000</pubDate>
        <link>http://charlesfrye.github.io/external/2022/08/10/fsdl-labs-overview.html</link>
        <guid isPermaLink="true">http://charlesfrye.github.io/external/2022/08/10/fsdl-labs-overview.html</guid>
        
        
        <category>external</category>
        
      </item>
    
      <item>
        <title>Reading Group on &quot;Designing ML Systems&quot;</title>
        <description>&lt;p style=&quot;text-align: center&quot;&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=K-SuQbmttbg&amp;amp;list=PLfeYlJzwvDN3R_xBwA8pp0cLOzxndlC4X&amp;amp;index=1&quot;&gt;&lt;img src=&quot;https://charlesfrye.github.io/img/dmls-cover.png&quot; alt=&quot;dmls-cover&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;In this book, you’ll learn a holistic approach
to designing ML systems that are reliable, scalable, maintainable,
and adaptive to changing environments and business requirements.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;!--exc--&gt;

&lt;p&gt;As part of the
&lt;a href=&quot;https://mlcollective.org/&quot;&gt;ML Collective&lt;/a&gt;
independent research organization,
I led a reading group of the soon-to-be-released O’Reilly book
&lt;a href=&quot;https://www.oreilly.com/library/view/designing-machine-learning/9781098107956/&quot;&gt;&lt;em&gt;Designing ML Systems&lt;/em&gt;&lt;/a&gt;,
by
&lt;a href=&quot;https://huyenchip.com/&quot;&gt;Chip Huyen&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can catch the recordings on
&lt;a href=&quot;https://www.youtube.com/watch?v=K-SuQbmttbg&amp;amp;list=PLfeYlJzwvDN3R_xBwA8pp0cLOzxndlC4X&amp;amp;index=1&quot;&gt;YouTube here&lt;/a&gt;.&lt;/p&gt;

</description>
        <pubDate>Sat, 12 Mar 2022 00:00:00 +0000</pubDate>
        <link>http://charlesfrye.github.io/external/2022/03/12/designing-ml-systems.html</link>
        <guid isPermaLink="true">http://charlesfrye.github.io/external/2022/03/12/designing-ml-systems.html</guid>
        
        
        <category>external</category>
        
      </item>
    
      <item>
        <title>Advent of Code in Three Languages</title>
        <description>&lt;h3 id=&quot;different-programming-languages-push-us-to-write-code-differently&quot;&gt;Different programming languages push us to write code differently.&lt;/h3&gt;

&lt;p&gt;Python pushed me to write clean code.&lt;/p&gt;

&lt;p&gt;Rust pushed me to write performant code.&lt;/p&gt;

&lt;p&gt;Haskell pushed me to solve problems differently.&lt;/p&gt;

&lt;!--exc--&gt;

&lt;h2 id=&quot;three-borrowed-refs-two-left-folds--a-python-in-a-binary-tree&quot;&gt;Three Borrowed Refs, Two Left Folds, &amp;amp; A Python In A Binary Tree&lt;/h2&gt;

&lt;p&gt;Every year,
&lt;a href=&quot;http://adventofcode.com/&quot;&gt;Advent of Code&lt;/a&gt;
puts out 25 coding puzzles in December,
one a day until Christmas.&lt;/p&gt;

&lt;p&gt;I &lt;a href=&quot;https://github.com/charlesfrye/AdventOfCode&quot;&gt;first did AoC in 2015&lt;/a&gt;,
when I was just starting to seriously learn Python,
coming from a scientific/statistical computing
background in MATLAB and R.&lt;/p&gt;

&lt;p&gt;Since then, I haven’t gone back to AoC.
My Decembers were filled with writing papers,
not code,
and my free time was spent learning math,
not programming.&lt;/p&gt;

&lt;p&gt;This year I had some extra free time on my hands,
so I decided to go back to Advent of Code.&lt;/p&gt;

&lt;p&gt;And this time around, I decided to work through the problems
in three different languages:
&lt;a href=&quot;https://automatetheboringstuff.com/&quot;&gt;Python&lt;/a&gt;,
&lt;a href=&quot;https://web.archive.org/web/20211125090205/http://learnyouahaskell.com/chapters&quot;&gt;Haskell&lt;/a&gt;, and
&lt;a href=&quot;https://doc.rust-lang.org/rust-by-example/&quot;&gt;Rust&lt;/a&gt;.
I used &lt;a href=&quot;http://replit.com/&quot;&gt;replit&lt;/a&gt;
to get access to persistent, web-accessible environments
for all three languages in a unified interface.
Links to executable versions of my solutions, hosted on replit,
are sprinkled throughout this post.
You can find all of the solutions
&lt;a href=&quot;https://replit.com/@charlesfrye/&quot;&gt;here&lt;/a&gt;,
named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XX - langName&lt;/code&gt; for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XX&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;01..08&lt;/code&gt;
and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;langName&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Python&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rust&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Haskell&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Python is a loosey-goosey language:
it’s
&lt;a href=&quot;https://www.sciencedirect.com/topics/computer-science/interpreted-language&quot;&gt;interpreted&lt;/a&gt;,
&lt;a href=&quot;https://realpython.com/lessons/dynamic-vs-static/&quot;&gt;dynamically-typed&lt;/a&gt;,
and
&lt;a href=&quot;https://betterprogramming.pub/garbage-collection-in-python-6dca154ae1dd&quot;&gt;garbage-collected&lt;/a&gt;.
Haskell is much less loose, but is even more abstract,
in the sense of being far from the metal:
it’s got a
&lt;a href=&quot;https://vitez.me/hts-language&quot;&gt;strong type system&lt;/a&gt;,
works &lt;a href=&quot;https://stackoverflow.com/questions/40130014/why-is-haskell-fully-declarative&quot;&gt;declaratively&lt;/a&gt;,
and evaluates &lt;a href=&quot;https://www.schoolofhaskell.com/school/starting-with-haskell/basics-of-haskell/3-pure-functions-laziness-io&quot;&gt;pure functions lazily&lt;/a&gt;.
Neither language foregrounds performance or memory management,
unlike Rust, a compiled
&lt;a href=&quot;https://stackoverflow.blog/2020/01/20/what-is-rust-and-why-is-it-so-popular/&quot;&gt;systems programming language&lt;/a&gt;
that uses
&lt;a href=&quot;https://blog.logrocket.com/introducing-the-rust-borrow-checker/&quot;&gt;borrow checking&lt;/a&gt;
to make memory maintenance easier.&lt;/p&gt;

&lt;p&gt;Before starting this process,
I was &lt;a href=&quot;https://replit.com/@charlesfrye/LazyPython4HardworkingCoders?v=1&quot;&gt;deeply&lt;/a&gt;
familiar with Python,
at an early-intermediate level with Haskell,
and completely new to Rust.&lt;/p&gt;

&lt;p&gt;Working through the Advent of Code problems in three very different languages,
each of which I have very different familiarity with,
was an incredible educational experience.
My knowledge of all three languages,
and of programming in general,
was greatly deepened.&lt;/p&gt;

&lt;p&gt;This blog post summarizes what I learned from that experience,
as best I can.&lt;/p&gt;

&lt;p&gt;Here are my main tl;drs:
two truisms about programming languages in general,
plus the specifics for each language,
and a note about using Advent of Code and puzzles
to learn a language.
After covering each of these in detail,
with examples,
I’ll closes out with some smaller take-aways about tooling.&lt;/p&gt;

&lt;h3 id=&quot;different-programming-languages-push-us-to-write-code-differently-1&quot;&gt;Different programming languages push us to write code differently.&lt;/h3&gt;

&lt;p&gt;Python pushed me to write clean code.&lt;/p&gt;

&lt;p&gt;Rust pushed me to write performant code.&lt;/p&gt;

&lt;p&gt;Haskell pushed me to solve problems differently.&lt;/p&gt;

&lt;h3 id=&quot;each-language-has-its-strengths-and-weaknesses&quot;&gt;Each language has its strengths and weaknesses.&lt;/h3&gt;

&lt;p&gt;Python is easy (but slow).&lt;/p&gt;

&lt;p&gt;Haskell is hard (but beautiful).&lt;/p&gt;

&lt;p&gt;Rust is fast and easy to learn (but abstraction is hard).&lt;/p&gt;

&lt;h3 id=&quot;doing-lots-of-puzzles-quickly-ingrained-some-bad-habits&quot;&gt;Doing lots of puzzles quickly ingrained some bad habits.&lt;/h3&gt;

&lt;p&gt;I ended up copy-pasting lots of code, especially boilerplate.&lt;/p&gt;

&lt;p&gt;I ended up writing lots of solutions that “just worked”.&lt;/p&gt;

&lt;h2 id=&quot;different-programming-languages-push-us-to-write-code-differently-2&quot;&gt;Different programming languages push us to write code differently.&lt;/h2&gt;

&lt;p&gt;The basic version of this truism
predates computers.
There’s a possibly apocryphal quote,
supposedly of Nietzsche,
the late 19th-century German philosopher,
on beginning to work with a typewriter:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;As our minds are working on our tools, so our tools are working on our minds.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Programming languages are, alongside hardware and developer environments,
among the core tools of a programmer, so they work very intensely on our minds.
Their assumptions become our biases and their happy paths become our preferred routes.&lt;/p&gt;

&lt;p&gt;Working on the same problems in three very different languages
in short succession made this evident,
but it also suggested ways to bring the strengths of each language
into work done in the others.&lt;/p&gt;

&lt;h3 id=&quot;python-pushed-me-to-write-clean-code&quot;&gt;Python pushed me to write clean code.&lt;/h3&gt;

&lt;p&gt;Python uses whitespace to organize code
and does not require type declarations,
so there’s a minimum of “cruft”
around the core logic of your code.&lt;/p&gt;

&lt;p&gt;Consider this example,
from &lt;a href=&quot;https://replit.com/@charlesfrye/06-Python&quot;&gt;my Python solution to Day 6&lt;/a&gt;.
On Day 6, we’re asked to
&lt;a href=&quot;https://adventofcode.com/2021/day/6&quot;&gt;simulate the population growth of lanternfish&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://charlesfrye.github.io/img/aoc2021_06_python.png&quot; alt=&quot;aoc2021_06_python&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The instructions of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; function read almost
like a prose description of (or at least pseudocode for) the program:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;read&lt;/code&gt; a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file&lt;/code&gt;,&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parse&lt;/code&gt; the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;contents&lt;/code&gt;,&lt;/li&gt;
  &lt;li&gt;then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;run&lt;/code&gt; the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lanternfish_simulator&lt;/code&gt; and&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt; the fish up.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the absence of visual distractors,
like brackets or variable annotations like type and mutability,
the source code only contains the instructions we are giving the computer.
For me, this encourages a “readability discipline”:
I want the gist of the program to be as clear to a reader
as the execution of it is to the computer.&lt;/p&gt;

&lt;p&gt;As I worked on my Rust and Haskell solutions,
I tried to maintain this discipline.
In Rust, I avoided manually inlining as much as I could,
so that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; function,
and the functions it calls directly, would be as short as possible.
In Haskell, I used &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;let&lt;/code&gt; blocks to organize the calculations
in my programs, with clear and descriptive names for variables and functions.&lt;/p&gt;

&lt;p&gt;This influence is clearly visible in the
soltutions for the same day
in Rust
(&lt;a href=&quot;https://replit.com/@charlesfrye/06-Rust&quot;&gt;executable source code on replit&lt;/a&gt;)
and Haskell
(&lt;a href=&quot;https://replit.com/@charlesfrye/06-Haskell&quot;&gt;executable source code on replit&lt;/a&gt;):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://charlesfrye.github.io/img/aoc2021_06_rust.png&quot; alt=&quot;aoc2021_06_rust&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://charlesfrye.github.io/img/aoc2021_06_haskell.png&quot; alt=&quot;aoc2021_06_haskell&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;rust-pushed-me-to-write-performant-code&quot;&gt;Rust pushed me to write performant code.&lt;/h3&gt;

&lt;p&gt;Rust, on the other hand, was constantly reminding me of
the performance cost of my code: with
every cloned value from the heap,
every pass over an iterator,
every additional layer of functional abstraction,
I could feel that I was adding weight to my program.&lt;/p&gt;

&lt;p&gt;Contrary to the “human-readability” bias
I was absorbing from Python,
Rust’s constant emphasis –
in the type system, the syntax, and the standard library –
on just exactly what the computer would be doing during execution
encouraged me to try and make things simpler and easier
from the computer’s perspective.
If a function would only be called once,
why not inline it and save the extra weight of borrowing
or of specifying additional types?&lt;/p&gt;

&lt;p&gt;Outside of specialized, highly performance-sensitive contexts,
I think this is a bad instinct.
But the instincts of Rust aren’t all bad.
When turning to Python or Haskell from Rust,
I found myself noticing more often when I was
triggering an expensive operation like a copy
or (especially in Python) iterating over a data structure
more than once unnecessarily.&lt;/p&gt;

&lt;p&gt;As a novice Rust programmer,
I was particularly scared of functional abstraction,
because it required more thoughtful and deep engagement
with Rust concepts like borrowing,
&lt;a href=&quot;https://doc.rust-lang.org/book/ch19-05-advanced-functions-and-closures.html&quot;&gt;closures&lt;/a&gt;, and
&lt;a href=&quot;https://stackoverflow.com/questions/30177395/when-does-a-closure-implement-fn-fnmut-and-fnonce&quot;&gt;function types&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This experience couldn’t be more different from the experience
of writing Haskell.&lt;/p&gt;

&lt;h3 id=&quot;haskell-pushed-me-to-solve-problems-differently&quot;&gt;Haskell pushed me to solve problems differently.&lt;/h3&gt;

&lt;p&gt;Unlike Rust, functional abstraction is effortless in Haskell.&lt;/p&gt;

&lt;p&gt;For example, currying, where a function of two arguments
is transformed into a function of one argument is
&lt;a href=&quot;https://internals.rust-lang.org/t/currying-in-rust/10326&quot;&gt;hard in Rust&lt;/a&gt;.
Currying in Haskell is like water to fish:
any “function of two arguments” in Haskell&lt;/p&gt;
&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;is actually a function of one argument that returns
a function of one argument.&lt;/p&gt;
&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So instead of working primarily by manipulating &lt;em&gt;values&lt;/em&gt;,
as in Rust and Python,
Haskell encourages the programmer to manipulate &lt;em&gt;functions&lt;/em&gt;.
Solutions or levels of abstraction that would be fiendishly
complex in an imperative, value-centric paradigm become simple and natural
in a functional paradigm (and vice versa).&lt;/p&gt;

&lt;p&gt;Because I learned programming imperatively (MATLAB, R, and Python),
my first instinct on seeing a problem was to solve it
in an imperative way: generating and mutating finite data structures.&lt;/p&gt;

&lt;p&gt;With Haskell, I found that I needed completely different solutions.
Advent of Code problems mostly involve taking a text file as input
and producing a single number or string as output –
this structural constraint makes it easier for the organizers
to implement randomization of inputs for individual participants
and to allow easy entry and checking of answers in a language-independent way.&lt;/p&gt;

&lt;p&gt;From a Haskell perspective, that means all of the problems
can be solved by composing two pieces:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;a parser that creates a data structure from the input and&lt;/li&gt;
  &lt;li&gt;a &lt;a href=&quot;https://blog.sumtypeofway.com/posts/recursion-schemes-part-2.html&quot;&gt;catamorphism&lt;/a&gt;
that consumes the data structure and spits out the answer –
for the puzzles I completed, typically a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fold&lt;/code&gt; on a list.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Framing my solutions in this way often led to very different solutions
in Haskell than in Rust or Python – some of which were better.&lt;/p&gt;

&lt;p&gt;For example, on Day 7 we are asked to
&lt;a href=&quot;https://adventofcode.com/2021/day/7&quot;&gt;compute the cost-minimizing position for some submarines&lt;/a&gt;,
which happen to be driven by crabs
(Advent of Code is whimiscal like that).
In my
&lt;a href=&quot;https://replit.com/@charlesfrye/07-Rust&quot;&gt;Rust&lt;/a&gt;
and &lt;a href=&quot;https://replit.com/@charlesfrye/07-Python&quot;&gt;Python&lt;/a&gt;
solutions, I immediately reached for arrays:
calculate the cost for each position for each submarine
(then sum up and find the min/argmin).
This solution was \(O(n^2)\) – note the nested “for each”.&lt;/p&gt;

&lt;p&gt;It’s perfectly feasible to program this solution in Haskell
(probably as an
&lt;a href=&quot;https://hackage.haskell.org/package/lens-3.7/docs/Control-Lens-IndexedTraversal.html&quot;&gt;indexed traversal&lt;/a&gt;),
but it didn’t feel “natural”.&lt;/p&gt;

&lt;p&gt;Looking more closely at the problem,
it was clear that the functions being optimized were convex:
they had only one “change of direction” from decreasing to increasing.
This gives an \(O(n)\) algorithm:
iteratively increase the starting position until the
function stops decreasing.&lt;/p&gt;

&lt;p&gt;While that sounds like a stateful algorithm,
which would not be particularly idiomatic Haskell,
it has a clean and terse functional equivalent:
apply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;drop&lt;/code&gt; to an infinite list of costs,
returning the first element whose successor is greater.
So that’s what I did in
&lt;a href=&quot;https://replit.com/@charlesfrye/07-Haskell&quot;&gt;my solution&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This completely different perspective on the problem
both resulted from a deeper understanding of what I was doing and
led to a speedup from quadratic to linear time complexity
(and from quadratic to constant space complexity).&lt;/p&gt;

&lt;p&gt;When working in the other languages,
my thinking didn’t even come close to this solution.
But importantly, once I found it,
transferring this solution to Rust or Python
would be straightforward.&lt;/p&gt;

&lt;p&gt;Working with a language, like Haskell,
that operates in a paradigm
completely different to the norm
is a great way to uncover totally different ways to solve problems –
something much deeper than the cosmetic and optimization differences
between working with Rust and Python.&lt;/p&gt;

&lt;h2 id=&quot;each-language-has-its-strengths-and-weaknesses-1&quot;&gt;Each language has its strengths and weaknesses.&lt;/h2&gt;

&lt;p&gt;This truism bears repeating if only because
programmers often become chauvinists
for their first language, or for the latest hotness,
or for the time-worn and battle-tested:
there is no one tool that is best for all jobs.&lt;/p&gt;

&lt;p&gt;Tools that succeed succeed because they solve
at least some problems well in some circumstances;
tools that succeed are doomed to fail
on at least some problems in some circumstance.&lt;/p&gt;

&lt;h3 id=&quot;python-is-easy-but-slow&quot;&gt;Python is easy (but slow).&lt;/h3&gt;

&lt;p&gt;First, I should admit I am biased:
I am most comfortable in Python,
which has for me been both an educational environment for
&lt;a href=&quot;https://www.nand2tetris.org/&quot;&gt;learning programming concepts&lt;/a&gt;
and
&lt;a href=&quot;https://charlesfrye.github.io/programming/2019/02/22/multiplication-convoluted-part-two.html&quot;&gt;fleshing out mathematical ideas&lt;/a&gt;
and the high-level interface
to quickly build numerical programs,
from
&lt;a href=&quot;https://charlesfrye.github.io/psych101d&quot;&gt;Markov Chain Monte Carlo for statistical computing&lt;/a&gt;
to
&lt;a href=&quot;https://arxiv.org/abs/2003.10397&quot;&gt;deep neural network training&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That said, Python just &lt;em&gt;is&lt;/em&gt; easier than Rust or Haskell,
for inherent and for cultural reasons:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;It’s easy to refactor, since there’s less text
(e.g. type signatures, variable annotations, syntax)
to change around.&lt;/li&gt;
  &lt;li&gt;It’s easy to switch programming paradigms on the fly,
since there’s good support inside the standard library
for different programming paradigms.&lt;/li&gt;
  &lt;li&gt;The language’s popularity and maturity mean there’s a plethora
of libraries that include specialized data structures and algorithms, like
&lt;a href=&quot;https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.label.html&quot;&gt;this function from scipy&lt;/a&gt;
that was essentially a
&lt;a href=&quot;https://replit.com/@charlesfrye/09-Python&quot;&gt;one-line solution to Day 9&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Similarly, that popularity and maturity mean that there’s also a
plethora of high quality tutorials, blog posts, and Stack Overflow discussions
where you can learn how to solve particular problems
or use certain tools and libraries.
I found myself particularly missing these resources when working in Haskell.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Of course, the flexibility and lack of rigor
in the program specification also means
it’s easier to write an &lt;em&gt;incorrect&lt;/em&gt; Python program that still runs.
I think one reason Python feels particularly easy to use
for Advent of Code challenges is that they are a form of
&lt;a href=&quot;https://en.wikipedia.org/wiki/Test-driven_development&quot;&gt;test-driven development&lt;/a&gt;.
In the Platonic ideal of TDD, tests are written before code,
and the code is finished when it passes the tests.
That’s how Advent of Code works:
the submission acts as a test,
and the problem statements typically include
lots of results to test your code against –
all written down and then read by the programmer
before any code hits a text file.&lt;/p&gt;

&lt;p&gt;The other cost to this ease of use is execution speed.
Consistently, the compiled Haskell and Rust programs
would be effectively instantaneous –
bound by printing to the terminal for a human to read the result,
rather than by internal computation.
Not so for Python,
where there was often a slight but noticeable delay
while the program executed.&lt;/p&gt;

&lt;p&gt;Fundamentally, that’s because Python is
interpreted rather than compiled,
uses a garbage collector to manage memory,
and has limited thread-wise parallelization.
These are core language features that are
very difficult to work around.&lt;/p&gt;

&lt;p&gt;That makes Python unsuitable for a lot of domains.
But for many problems, this speed difference is not a dealbreaker,
just as it was not a dealbreaker for Advent of Code.
When the Rust (and sometimes Haskell) programs would terminate
in well under a second,
the Python programs would terminate within a few seconds.
For a program that only really needs to be run once,
like these Advent of Code solutions,
that’s an extremely tolerable cost;
the same likely holds true for other applications,
like running a job on a minutely/hourly/daily schedule.&lt;/p&gt;

&lt;h3 id=&quot;haskell-is-hard-but-beautiful&quot;&gt;Haskell is hard (but beautiful).&lt;/h3&gt;

&lt;p&gt;I’ve been learning Haskell on and off for
&lt;a href=&quot;https://github.com/charlesfrye/LearnYouAHaskell&quot;&gt;a few years now&lt;/a&gt;.
I’ve treated it as a way to learn
more about category theory and abstract algebra
than as a new programming language for solving concrete problems.&lt;/p&gt;

&lt;p&gt;In that time, it has become easier to write Haskell programs,
but I still find it challenging.
In fact, even though I just started learning Rust
within the last month,
I often found it easier to write a Rust solution
than a Haskell solution.
I think there are two core reasons.&lt;/p&gt;

&lt;p&gt;The lesser of the two reasons is that the dev tooling for Haskell
is not great, compared to the tools for Python and Rust.
See the Minor Take-Aways section at the end of this post for more on this.&lt;/p&gt;

&lt;p&gt;The bigger reason is that Haskell is a declarative language,
rather than an imperative language,
and imperative languages are more “natural”.&lt;/p&gt;

&lt;p&gt;They are more natural in two senses.&lt;/p&gt;

&lt;p&gt;First, imperative languages are clearly closer to how a computer runs.
At the core of a computer, registers that contain state
are operated on to produce new states,
in one step after another.
This is imperative programming par excellence.
The gap between declarative Haskell source code
and the final imperative program
(which is closed by the compiler)
makes it very difficult to reason about some aspects
of Haskell program behavior,
as in the notorious
&lt;a href=&quot;https://www.reddit.com/r/haskell/comments/pvosen/how_can_haskell_programmers_tolerate_space_leaks/&quot;&gt;space leaks&lt;/a&gt;
that are
&lt;a href=&quot;https://wiki.haskell.org/Foldr_Foldl_Foldl&apos;&quot;&gt;caused by laziness&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Second, imperative languages are, more speculatively,
closer to natural human thought.
Human brains did not evolve to write proofs or programs;
they evolved to maximize survival and reproduction
in a world of prey, predators, and packmates.
These are agents who take actions in an environment –
statefulness, mutability, and time are first-class citizens.
We express our thoughts in sentences
that combine state (nouns) with action (verbs).
This closer match between the structure of imperative programs
(“do this, then that”)
and the structure of our intuitive thought
makes it easier for us to work with them.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://bartoszmilewski.com/&quot;&gt;Bartosz Milewski&lt;/a&gt;
in his YouTube lecture series
&lt;a href=&quot;https://www.youtube.com/watch?v=3XTQSx1A3x8&amp;amp;list=PLbgaMIhjbmElia1eCEZNvsVscFef9m0dm&amp;amp;ab_channel=BartoszMilewski&quot;&gt;Category Theory for Programmers II&lt;/a&gt;,
draws an illustrative analogy here,
drawing from mathematical physics.&lt;/p&gt;

&lt;p&gt;He likens imperative programming to the
Hamiltonian approach to mechanics.
Hamiltonians are used to derive differential equations
that predict the motion and behavior of a system.
A differential equation is like a imperative program:
at each point in time, it gives an update to the value of a state,
which is then used to update the state at the next point in time.&lt;/p&gt;

&lt;p&gt;Declarative programming, on the other hand, is more like the
Lagrangian approach to mechanics.
Solving for the paths of systems in Lagrangian mechanics
is based on minimization:
the path a system follows is whichever one minimizes
a quantity called the action.
There is no obvious flow of time – the entire path
is solved for at once.
In English, you might declare “let the path be that which minimizes the action”;
in Haskell, this becomes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;let path = argmin action paths&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;By the way: if you’re not well-versed in physics,
the duality between the Hamiltonian and Lagrangian views
of the world is both explained and dramatized beautifully by
author Ted Chiang in his short story
&lt;a href=&quot;http://raley.english.ucsb.edu/wp-content/uploads/Reading/Chiang-story.pdf&quot;&gt;“Story of Your Life”&lt;/a&gt;,
which was the basis for the 2016 movie
&lt;a href=&quot;https://en.wikipedia.org/wiki/Arrival_(film)&quot;&gt;&lt;em&gt;Arrival&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Critically, these approaches are &lt;em&gt;dual&lt;/em&gt;:
every mechanical system admits a description from both viewpoints.
From one, the other can be &lt;a href=&quot;https://phys.libretexts.org/Bookshelves/Classical_Mechanics/Variational_Principles_in_Classical_Mechanics_(Cline)/08%3A_Hamiltonian_Mechanics/8.02%3A_Legendre_Transformation_between_Lagrangian_and_Hamiltonian_mechanics&quot;&gt;mechanically derived&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The derivation or transformation from a declarative, stateless form
to an imperative, stateful form is precisely what the compiler or interpreter
of a declarative language does –
in the end, our computers have inherited our bias towards the imperative view.&lt;/p&gt;

&lt;p&gt;So Haskell is hard –
hard in a way that, to me at least,
feels as fundamental as the structure of thought.
But there are decided benefits to grappling with its challenges.&lt;/p&gt;

&lt;p&gt;Almost every time I completed a solution in Haskell,
I felt like I had understood something fundamental and deep
about the problem I was solving –
like in writing the program I had
connected the specific problem to a broader class of problems
or even learned something entirely new about computer programming.&lt;/p&gt;

&lt;p&gt;For example, on completing the
&lt;a href=&quot;https://adventofcode.com/2021/day/8&quot;&gt;seven-segment display challenge&lt;/a&gt;
on Day 8
&lt;a href=&quot;https://replit.com/@charlesfrye/08-Python&quot;&gt;in Python&lt;/a&gt;,
I felt frustrated –
I had hacked together a solution that worked
but had no deep explanation for why.&lt;/p&gt;

&lt;p&gt;The core of the challenge is to match each of ten provided strings
to a digit, based on the patterns of a seven-segment display
(as on a digital alarm clock).&lt;/p&gt;

&lt;p&gt;In Python, I used repeated iteration of a few heuristics
to narrow down which string went with each digit.&lt;/p&gt;

&lt;p&gt;When I tackled the question a second time,
using Haskell, I had to take
&lt;a href=&quot;https://replit.com/@charlesfrye/08-Haskell&quot;&gt;a completely different approach&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I was inspired, by
&lt;a href=&quot;https://twitter.com/liveinwood0422/status/1475188742608789504&quot;&gt;an answer I found on Twitter&lt;/a&gt;,
to effectively define each digit – declare which one it was –
rather than worry about the procedure for calculating it.&lt;/p&gt;

&lt;p&gt;This approach shows up in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;where&lt;/code&gt; block in the code below.
For example, the string for the digit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;five&lt;/code&gt; is the one that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;intersects&lt;/code&gt;
that for the digit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;six&lt;/code&gt; 5 times (and is one of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stringsOfLength 5&lt;/code&gt;).
The identity of the string for the digit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;six&lt;/code&gt; is similarly defined.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://charlesfrye.github.io/img/aoc2021_08_haskell.png&quot; alt=&quot;aoc2021_08_haskell&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Because of the lazy and declarative semantics of Haskell,
these values can be defined in totally different orders
in each execution –
when given one set of ten strings as input,
the identities of the digits might be solved in a completely different order
(first &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;four&lt;/code&gt;, then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nine&lt;/code&gt;; or first &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;seven&lt;/code&gt;, then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;four&lt;/code&gt;, then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zero&lt;/code&gt;, and so on).&lt;/p&gt;

&lt;p&gt;All that matters is that the terms are properly defined,
with no cyclical or missing definitions.
When I
&lt;a href=&quot;https://replit.com/@charlesfrye/08-Rust&quot;&gt;translated this solution into Rust&lt;/a&gt;,
I had to be precise about the order in which the solution had to be computed.&lt;/p&gt;

&lt;p&gt;Lastly, because I was wrapping these solutions up into functions
(the lambda term at the top of the screenshot above),
the close connection between lambdas, closures,
and
&lt;a href=&quot;https://github.com/BartoszMilewski/Publications/blob/afee45b435f032ff97ec5cb9da644b66775665bf/TheDaoOfFP/6-FunctionTypes.pdf&quot;&gt;data constructors for function types&lt;/a&gt;
was made manifest:
lambdas act like data constructors for function types,
using currying to map a function defined on the pair of context and argument
into a function that takes a context and returns a function that takes an argument –
effectively closing over the context in which the lambda was defined.&lt;/p&gt;

&lt;p&gt;In the end, this was a solution I was proud of
and one from which I learned,
rather than an ugly hack that made me feel frustrated.&lt;/p&gt;

&lt;p&gt;That was my general experience with writing solutions in Haskell.
Each one was a gem – transparently clear, austerely beautiful (to their mother),
and hard-won by toil with pick and polish.&lt;/p&gt;

&lt;h3 id=&quot;rust-is-fast-and-easy-to-learn-but-abstraction-is-hard&quot;&gt;Rust is fast and easy to learn (but abstraction is hard).&lt;/h3&gt;

&lt;p&gt;As noted above,
I only started learning Rust just before starting
Advent of Code.
After a month, I can already say it’s
one of my favorite languages to write code in.&lt;/p&gt;

&lt;p&gt;Despite being a systems programming language,
Rust is easy to learn:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;The
&lt;a href=&quot;https://doc.rust-lang.org/rust-by-example/&quot;&gt;Rust By Example&lt;/a&gt;
(RBE) document is one of the best ways to learn
programming I’ve ever come across.
It mixes clear and pedagogical exposition
with links to reference documentation and
(critically) editable, executable code.
This executable code is backed by
&lt;a href=&quot;https://webassembly.org/&quot;&gt;Web Assembly&lt;/a&gt;
(Wasm),
with which Rust plays very nicely.
RBE demonstrates just how useful and powerful
Wasm can be – and so also why one should learn Rust.&lt;/li&gt;
  &lt;li&gt;The Rust compiler is actually your friend.
Error messages from compilers and interpreters are notoriously
arcane and useless.
After a lot of exposure, I can just barely grok the output of
the error messages from GHC, the primary Haskell compiler.
The error messages in Python’s standard library are
similarly less helpful than they could be.
The Rust compiler, by contrast,
typically gives extremely clear error messages
that identify where in a line the error is
and how to fix it,
with links to additional detailed documentation.
In conversation with the compiler,
a novice in the language (like me)
can take a very broken program,
with errors in variable ownership,
mutability, type matching, syntax, and more,
and get it running.&lt;/li&gt;
  &lt;li&gt;Rust clearly learns from other popular languages.
Much of the syntax and nomenclature echoes Python,
but without the unpopular significant whitespace.
The type system has shades of Haskell as well,
and Rust can also automatically &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;derive&lt;/code&gt; some traits.
As a systems language, it also draws heavily from C and C++.
This makes it feel familiar from almost any programming background.&lt;/li&gt;
  &lt;li&gt;As the
&lt;a href=&quot;https://stackoverflow.blog/2020/01/20/what-is-rust-and-why-is-it-so-popular/&quot;&gt;most-loved language on StackOverflow&lt;/a&gt;,
Rust gets a lot of love on that platform,
meaning there are lots of well-answered questions
and accompanying code snippets.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And this is all while being blazing fast:
when compiling Rust programs in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--release&lt;/code&gt; mode,
the results were uniformly extremely snappy.&lt;/p&gt;

&lt;p&gt;There’s one drawback that I noticed:
functional abstraction is not easy.
It’s not exceedingly difficult to do maps and folds –
you can get pretty far by passing basic functions or lambdas
into iterator interfaces.
Function types are needed to work seriously with higher-order functions,
and these interact in a more complex way than I expected
with the borrow checker.
I found myself copying and pasting code I didn’t understand
much more often, along with blindly following the compiler’s suggestions.&lt;/p&gt;

&lt;h2 id=&quot;doing-lots-of-puzzles-quickly-ingrained-some-bad-habits-1&quot;&gt;Doing lots of puzzles quickly ingrained some bad habits.&lt;/h2&gt;

&lt;p&gt;The quick pace and goal-orientation of the puzzles was good:
it kept me moving, gave me clear feedback on success,
and prevented me from spending too much time on abstraction.&lt;/p&gt;

&lt;p&gt;But it also encouraged me to cut corners in ways that were harmful for learning.&lt;/p&gt;

&lt;h3 id=&quot;i-ended-up-copy-pasting-lots-of-code-especially-boilerplate&quot;&gt;I ended up copy-pasting lots of code, especially boilerplate.&lt;/h3&gt;

&lt;p&gt;In Haskell, I didn’t learn as much about monads and IO as I would’ve liked
because I just used the same format for each program.
I also didn’t learn how to do clean argument parsing.
I also stuck with the fairly janky and unergonomic Haskell Cabal setup
provided by replit without diving deeper into how it worked.&lt;/p&gt;

&lt;p&gt;In Rust, I slapped together a barebones argument-parsing and file-reading approach
on the first day and then copy-pasted it into subsequent solutions.
In both cases, I didn’t come away with the knowledge required
to write a clean CLI tool in the language.&lt;/p&gt;

&lt;p&gt;For all three languages,
I settled on a basic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bash&lt;/code&gt; script for running all the programs
in the first day or two, then didn’t improve it.
It would’ve been better to e.g. use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make&lt;/code&gt; to reduce compilaton overhead
and look into more of replit’s features.&lt;/p&gt;

&lt;h3 id=&quot;i-ended-up-writing-lots-of-solutions-that-just-worked&quot;&gt;I ended up writing lots of solutions that “just worked”.&lt;/h3&gt;

&lt;p&gt;I initially set myself the goal of doing every puzzle in each of three languages.
If I was really cooking, I could get a solution in Python in an hour or two,
but it was more typical to take three to five.&lt;/p&gt;

&lt;p&gt;There was a new puzzle each day,
so I was looking at 8+ hours of coding daily to keep up.
Even after cutting my target down to 1/3 of the puzzles in each language,
I was still looking at multiple hours of coding every day.&lt;/p&gt;

&lt;p&gt;This put pressure to “get stuff done” and “just ship it”,
rather than take time to learn.&lt;/p&gt;

&lt;p&gt;In Haskell, this meant that my refactoring was limited,
even though confident, safe refactoring from an MVP to a clean program
is one of the joys of that language.
I didn’t learn good “Safe Haskell” habits
(which is what most folks are thinking of when they say things like
“Haskell programs are guaranteed not to crash at runtime”).
I also didn’t learn about “Template Haskell”
for reducing boilerplate and
I only scratched the surface of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Lens&lt;/code&gt;es
(pun and negative connotation intended).&lt;/p&gt;

&lt;p&gt;In Rust, I never did a deep dive on ownership/borrowing,
closures, or traits.
I have no idea what a macro is or how I would make my own.
Instead of looking into the reasoning for the compiler’s
suggested fixes, I just blindly accepted them,
and I likely tied my programs into knots
while doing so.&lt;/p&gt;

&lt;p&gt;In Python, I didn’t learn anything new – no new libraries or idioms.
I also didn’t practice the
&lt;a href=&quot;https://cjolowicz.github.io/posts/hypermodern-python-01-setup/&quot;&gt;hypermodern style&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;minor-take-aways&quot;&gt;Minor Take-Aways&lt;/h2&gt;

&lt;p&gt;Above were all the major take-aways.&lt;/p&gt;

&lt;p&gt;I’ll close out with some smaller things I learned.&lt;/p&gt;

&lt;h3 id=&quot;replit-is-great-for-prototyping&quot;&gt;replit is great for prototyping.&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://replit.com&quot;&gt;replit&lt;/a&gt;
provides persistent environments for development,
with an emphasis on education and hobby programming.&lt;/p&gt;

&lt;p&gt;It was straightforward to get set up with each language.&lt;/p&gt;

&lt;p&gt;It required a moderate amount of boilerplate and copying each time
(especially for Haskell, where tooling wasn’t as good),
but nothing too excessive.&lt;/p&gt;

&lt;p&gt;By the time I was really going with each language, though,
I wished for the full control and automation tools
that a more traditional environment would offer.
If I do this next year,
I’ll spend a day or two before the month starts setting up a nice
development env with a makefile, nesting, etc.,
and use GitHub for hosting.&lt;/p&gt;

&lt;p&gt;That said, it was wonderful to be able to setup envs quickly
and access them from any device,
with persistent storage.
One issue I ran into: on a particularly poor internet connection
(airplane WiFi, a phone tether),
replit became unstable and I had to switch to using
&lt;a href=&quot;http://colab.research.google.com/&quot;&gt;Google Colab&lt;/a&gt;,
which was a decidedly worse experience outside of Python.&lt;/p&gt;

&lt;h3 id=&quot;tooling-and-libraries-for-haskell-are-very-limited-especially-for-linear-algebra&quot;&gt;Tooling and libraries for Haskell are very limited, especially for linear algebra.&lt;/h3&gt;

&lt;p&gt;As you might expect for a more niche language,
the tooling for Haskell is not great.&lt;/p&gt;

&lt;p&gt;The Haskell Cabal replit template wasn’t great.
For example, it comes packaged with a setup of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cabal&lt;/code&gt;
built tool that requires a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v1-&lt;/code&gt; prefix for all commands (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cabal v1-build&lt;/code&gt;)
or else prints a warning.
There was also no real IDE integration in the editor.
There was no real syntax checking (outside of highlighting),
no type hinting or checking,
and no useful form of autocomplete.&lt;/p&gt;

&lt;p&gt;But it’s not just replit’s fault.
Haskell is not a broadly popular language,
so there’s less of everything,
even beyond tools:
StackOverflow discussions,
YouTube tutorials,
code snippets,
etc.&lt;/p&gt;

&lt;p&gt;And unfortunately for folks interested in learning to use Haskell,
the documentation for most libraries is poor,
especially in comparison to Rust.
I found myself looking at the source code,
which wasn’t always very clear.&lt;/p&gt;

&lt;p&gt;The prototypical example of this, in my experience,
was the
&lt;a href=&quot;https://hackage.haskell.org/package/linear&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear&lt;/code&gt; library&lt;/a&gt;,
which I reached for to get matrix math
without incurring a dependency on BLAS/LAPACK.
In part because there is no example code
and in part because the library is based
on the notoriously abstruse
&lt;a href=&quot;https://hackage.haskell.org/package/lens&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lens&lt;/code&gt; library&lt;/a&gt;,
it took me several hours of scanning source code
to figure out how to create vectors and matrices
and how to access their entries.&lt;/p&gt;

</description>
        <pubDate>Fri, 31 Dec 2021 00:00:00 +0000</pubDate>
        <link>http://charlesfrye.github.io/programming/2021/12/31/aoc-rust-haskell.html</link>
        <guid isPermaLink="true">http://charlesfrye.github.io/programming/2021/12/31/aoc-rust-haskell.html</guid>
        
        
        <category>programming</category>
        
      </item>
    
      <item>
        <title>A Public Dissection of a PyTorch Training Step</title>
        <description>&lt;p style=&quot;text-align: center&quot;&gt;&lt;a href=&quot;https://wandb.ai/wandb/trace/reports/A-Public-Dissection-of-a-PyTorch-Training-Step--Vmlldzo5MDE3NjU&quot;&gt;&lt;img src=&quot;https://charlesfrye.github.io/img/trace_screenshot.png&quot; alt=&quot;trace_screenshot&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;But now that Weights &amp;amp; Biases can render PyTorch traces using the Chrome Trace Viewer, I’ve decided to peel away the abstraction and find out just what’s been happening every time I call .forward and .backward. These traces indicate what work was being done and when in every process, thread, and stream on the CPU and GPU. By the time we’re done here, we’ll have a clear sense for why most of the optimization tips above work, plus the tools to make more informed choices about the performance of our PyTorch code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;!--exc--&gt;

&lt;p&gt;Today I released a blog post
using the PyTorch Trace Viewer tool
to look inside the forward and backward passes
of a simple neural network.&lt;/p&gt;

&lt;p&gt;If the Trace Viewer is like a microscope
for examining a DNN running on a CPU + GPU,
this blog post is a public dissection –
an examination with a pedagogical bent
and an emphasis on immediately obvious
and repeatable phenomena.&lt;/p&gt;

&lt;p&gt;Read it &lt;a href=&quot;https://wandb.ai/wandb/trace/reports/A-Public-Dissection-of-a-PyTorch-Training-Step--Vmlldzo5MDE3NjU&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
        <pubDate>Tue, 24 Aug 2021 00:00:00 +0000</pubDate>
        <link>http://charlesfrye.github.io/external/2021/08/24/trace-report.html</link>
        <guid isPermaLink="true">http://charlesfrye.github.io/external/2021/08/24/trace-report.html</guid>
        
        
        <category>external</category>
        
      </item>
    
  </channel>
</rss>
