<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Tripp Lyons</title><description>Technical notes by Tripp Lyons on LLM inference, machine learning systems, JAX, PyTorch, security write-ups, and engineering experiments.</description><link>https://tripplyons.com</link><language>en-us</language><item><title>Tailored inference: why one engine won&apos;t be fastest for everything</title><link>https://tripplyons.com/blog/tailored-inference</link><guid isPermaLink="true">https://tripplyons.com/blog/tailored-inference</guid><description>Why fitting inference software to a model, hardware, and workload can outperform a general-purpose serving engine.</description><pubDate>Thu, 22 Oct 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Z.ai got 3.22 times the throughput for GLM-5.3-Flash in 13 days, with the same model on the same chips, by changing only the software that serves it.&lt;/p&gt;
&lt;p&gt;I made a 6-minute video that explains how they did it, and why I think every team that serves models at scale should pay attention. The idea is called tailored inference: fitting the serving software to one model, on one kind of hardware, for the workload it will serve.&lt;/p&gt;
&lt;iframe style=&quot;width: 100%; aspect-ratio: 16 / 9; border: 0;&quot; src=&quot;https://www.youtube-nocookie.com/embed/2jUUpwk0xZI&quot; title=&quot;Tailored inference: why one engine won&amp;#x27;t be fastest for everything&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share&quot; referrerpolicy=&quot;strict-origin-when-cross-origin&quot; allowfullscreen&gt;&lt;/iframe&gt;
&lt;p&gt;In the video, I cover:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Why standard engines like vLLM and SGLang leave speed unused. They’re built to run almost anything, so they can’t take full advantage of any one combination.&lt;/li&gt;
&lt;li&gt;What the gains look like. DeepSeek’s DeepInfer runs one model and nearly halves the work of processing a prompt. vLLM got about 5 times the agent throughput on DeepSeek-V4.1-Flash after launch day, mostly from code for that one model. Databricks reports agent-written kernels 1.8 to 5.2 times faster than vLLM’s best.&lt;/li&gt;
&lt;li&gt;Why it’s happening now. AI agents do most of the tuning, so tailoring each new model is finally affordable.&lt;/li&gt;
&lt;li&gt;Where it goes wrong. Researchers at NVIDIA and UC Berkeley found that about a third of agent patches that passed unit tests failed in a real serving setup.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The question to ask isn’t which engine is fastest. It’s what’s fastest for this model, on this hardware, for this workload.&lt;/p&gt;
&lt;p&gt;At &lt;a href=&quot;https://makora.com/&quot;&gt;Makora&lt;/a&gt;, we find that speed with agents that search for optimizations for each model and hardware combination, checked against representative workloads before they ship.&lt;/p&gt;</content:encoded></item><item><title>Find a niche by intersecting your strengths</title><link>https://tripplyons.com/blog/intersecting-strengths</link><guid isPermaLink="true">https://tripplyons.com/blog/intersecting-strengths</guid><description>A practical way to find your niche by combining skills you already have.</description><pubDate>Tue, 17 Feb 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I recently wrapped up a job search and wanted to share a bit of advice. One thing that I think helped me stand out was finding areas of specialization by combining two of my strengths.&lt;/p&gt;
&lt;p&gt;List out all of your skills and find some combinations of areas that you can become great at. It is easier to stand out when you can find a more specific niche this way. Try to find roles that focus on a combination of two of your strengths.&lt;/p&gt;
&lt;p&gt;Here’s a Venn diagram showing how the intersection of two strengths is more specific than the strengths themselves:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://tripplyons.com/img/blog/intersecting-strengths/venn-diagram.svg&quot; alt=&quot;Venn Diagram&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;879&quot; height=&quot;681&quot;&gt;&lt;/p&gt;
&lt;p&gt;For example, here are some of the intersections I focused on during my recent job search:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;GPU Programming + AI Agents = AI-Assisted GPU Programming&lt;/li&gt;
&lt;li&gt;Reinforcement Learning + Fullstack Development = RL as a Service&lt;/li&gt;
&lt;li&gt;Zero Knowledge Proofs + Deep Learning = Zero Knowledge Machine Learning (ZKML)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I hope this idea can help you in your job search or areas of your life!&lt;/p&gt;</content:encoded></item><item><title>The DeepSeek V3.2 breakthrough simplified</title><link>https://tripplyons.com/blog/deepseek-sparse-attention</link><guid isPermaLink="true">https://tripplyons.com/blog/deepseek-sparse-attention</guid><description>An accessible walkthrough of DeepSeek Sparse Attention and how it reduces attention computation.</description><pubDate>Mon, 29 Sep 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;DeepSeek’s new &lt;a href=&quot;https://github.com/deepseek-ai/DeepSeek-V3.2-Exp/blob/main/DeepSeek_V3_2.pdf&quot;&gt;DeepSeek-V3.2-Exp paper&lt;/a&gt; presents a very clever method to speed up attention calculations.&lt;/p&gt;
&lt;h2 id=&quot;deepseek-sparse-attention&quot;&gt;DeepSeek Sparse Attention&lt;/h2&gt;
&lt;p&gt;Here is a basic outline of the new DeepSeek Sparse Attention (DSA) module used in their new model.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://tripplyons.com/img/blog/deepseek-sparse-attention/dsa-architecture.png&quot; alt=&quot;Attention architecture of DeepSeek-V3.2-Exp, with the lightning indexer and top-k selector highlighted in green&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1238&quot; height=&quot;650&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Figure 1 from the &lt;a href=&quot;https://github.com/deepseek-ai/DeepSeek-V3.2-Exp/blob/main/DeepSeek_V3_2.pdf&quot;&gt;DeepSeek-V3.2-Exp paper&lt;/a&gt;: DSA built on multi-latent attention. The green part shows the lightning indexer choosing which key-value entries the top-k selector passes to the core attention.&lt;/em&gt;&lt;/p&gt;
&lt;h3 id=&quot;submodule-1---lightning-indexer&quot;&gt;Submodule 1 - Lightning indexer&lt;/h3&gt;
&lt;p&gt;The lightning indexer uses a small attention-like computation to create an attention mask (a set of which tokens should “pay attention” to each other). Each row of the mask contains only the &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;k&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt; largest interactions between a given token’s query and all previous tokens’ keys.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;O(n^2)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt; (same class as attention)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The speedup compared to standard attention (also &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;O(n^2)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt;) is due to using fewer attention heads and fewer dimensions for each key and query vector.&lt;/p&gt;
&lt;h3 id=&quot;submodule-2---multi-latent-attention-mla&quot;&gt;Submodule 2 - Multi-latent attention (MLA)&lt;/h3&gt;
&lt;p&gt;A large multi-latent attention layer computes the output of the block, with one exception: for each query, it only computes the &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;k&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt; interactions that are included in the attention mask in a process known as sparse attention.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;O(kn)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt; where &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt; is the number of tokens and &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;k&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt; is the number of interactions to use.&lt;/p&gt;
&lt;h2 id=&quot;why-does-this-matter&quot;&gt;Why does this matter?&lt;/h2&gt;
&lt;p&gt;Although DeepSeek Sparse Attention technically has the same quadratic time complexity as attention, it is able to reduce the amount of time spent in the bottleneck by simply using a smaller submodule to check interactions. It then can reuse some of that information in a larger submodule.&lt;/p&gt;
&lt;p&gt;Figure 3 of the paper shows the effect on inference cost. The cost per million tokens still grows with token position, but it grows much more slowly for DeepSeek-V3.2-Exp than for DeepSeek-V3.1-Terminus.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://tripplyons.com/img/blog/deepseek-sparse-attention/inference-costs.png&quot; alt=&quot;Cost per million tokens by token position for DeepSeek-V3.1-Terminus and DeepSeek-V3.2-Exp during prefilling and decoding&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1250&quot; height=&quot;505&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Figure 3 from the &lt;a href=&quot;https://github.com/deepseek-ai/DeepSeek-V3.2-Exp/blob/main/DeepSeek_V3_2.pdf&quot;&gt;DeepSeek-V3.2-Exp paper&lt;/a&gt;: inference costs of DeepSeek-V3.1-Terminus and DeepSeek-V3.2-Exp on H800 clusters.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This kind of reused computation in attention layers reminds me of techniques such as &lt;a href=&quot;https://arxiv.org/abs/2405.05254&quot;&gt;YOCO&lt;/a&gt; (sharing a KV cache across layers) and &lt;a href=&quot;https://arxiv.org/abs/1911.02150&quot;&gt;Multi-Query Attention&lt;/a&gt; (sharing a KV cache across attention heads). What makes DeepSeek’s approach different is that it is reusing information about which entries of the attention matrix are more important.&lt;/p&gt;</content:encoded></item><item><title>A trick for backpropagation of linear transformations</title><link>https://tripplyons.com/blog/backprop-trick</link><guid isPermaLink="true">https://tripplyons.com/blog/backprop-trick</guid><description>How to derive gradients for linear transformations expressed with Einstein summation.</description><pubDate>Thu, 11 Sep 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Linear transformations such as sums, matrix products, dot products, Hadamard products, and many more can often be represented using an &lt;a href=&quot;https://numpy.org/doc/stable/reference/generated/numpy.einsum.html&quot;&gt;einsum&lt;/a&gt; (short for Einstein summation).&lt;/p&gt;
&lt;p&gt;This post explains a simple trick to backpropagate through any einsum, regardless of what operations it represents.&lt;/p&gt;
&lt;h2 id=&quot;example-einsum&quot;&gt;Example einsum&lt;/h2&gt;
&lt;p&gt;For example, an einsum for matrix multiplication can be written like so:&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np

A &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;arange&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# A = [&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# [0, 1, 2],&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# [3, 4, 5]&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# ]&lt;/span&gt;
B &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;arange&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# B = [&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# [0, 1, 2, 3],&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# [4, 5, 6, 7],&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# [8, 9, 10, 11]&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# ]&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# C = A @ B (matrix multiplication)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# calculate with einsum&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# A uses i and j, B uses j and k, and C uses i and k&lt;/span&gt;
C_einsum &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;einsum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;ij,jk-&gt;ik&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# C_einsum = [&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# [20, 23, 26, 29],&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# [56, 68, 80, 92]&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# ]&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# calculate with for loops&lt;/span&gt;
C_forloop &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# order of the loops doesn&apos;t matter&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# you can swap the order of the loops,&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# just like you can swap the order of some double integrals&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; j &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; k &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# A uses i and j, B uses j and k, and C uses i and k,&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# just like before with the einsum&lt;/span&gt;
            C_forloop&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# C_forloop = [&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# [20, 23, 26, 29],&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# [56, 68, 80, 92]&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;backpropagating-through-einsum&quot;&gt;Backpropagating through einsum&lt;/h2&gt;
&lt;p&gt;Here comes the fun part. Backpropagating through an einsum is easy with a simple trick.&lt;/p&gt;
&lt;p&gt;Let &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;L&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt; be the loss function, and assume we know &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∂&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∂&lt;/mi&gt;&lt;mi&gt;C&lt;/mi&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\frac{\partial L}{\partial C}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt; and need to compute &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∂&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∂&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\frac{\partial L}{\partial A}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;We can just swap what we did for &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;C&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;C&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt; with what we did for &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;A&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt;, keeping the letters the same for each tensor. The code should explain it best.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# forward pass&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# A uses i and j, B uses j and k, and C uses i and k&lt;/span&gt;
C &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;einsum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;ij,jk-&gt;ik&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# backward pass&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# dL_dC is computed somewhere in the backward pass&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# A uses i and j, B uses j and k, and C uses i and k&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# again, just like before with the original einsum&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# we swapped the letters for C with the letters for A&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# we used dL_dC instead of A for the first parameter&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# we computed dL_dA instead of C for the output&lt;/span&gt;
dL_dA &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;einsum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;ik,jk-&gt;ij&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dL_dC&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This simple swapping trick makes it very easy to create formulas when backpropagating through einsums.&lt;/p&gt;
&lt;h2 id=&quot;verifying-the-shape&quot;&gt;Verifying the shape&lt;/h2&gt;
&lt;p&gt;It has the same shape as &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;A&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt; because the output letters we used (i and j) during the backward pass correspond to the input letters for &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;A&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt; (i and j) during the forward pass.&lt;/p&gt;
&lt;h2 id=&quot;interpreting-the-einsum&quot;&gt;Interpreting the einsum&lt;/h2&gt;
&lt;p&gt;If you swap around the letters, you can more easily interpret the einsum used for backpropagation, as a kind of matrix multiplication.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# replacing j with k, and k with j&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# doesn&apos;t change the result&lt;/span&gt;
dL_dA &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;einsum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;ij,kj-&gt;ik&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dL_dC&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This corresponds to multiplying &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∂&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∂&lt;/mi&gt;&lt;mi&gt;C&lt;/mi&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\frac{\partial L}{\partial C}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt; by &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mo&gt;⊺&lt;/mo&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;B^\intercal&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt;. We can tell because it looks like the example einsum for matrix multiplication, except the letters for &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;B&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt; are swapped, meaning &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;B&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt; is transposed.&lt;/p&gt;
&lt;h2 id=&quot;verifying-the-values&quot;&gt;Verifying the values&lt;/h2&gt;
&lt;p&gt;We can use JAX’s automatic differentiation to verify the values.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; jax
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; jax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; jnp

&lt;span class=&quot;token comment&quot;&gt;# compute the loss&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    C &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jnp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;einsum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;ij,jk-&gt;ik&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; jnp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# compute the gradient of the loss with respect to A&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;grad_A&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# argnums=0 corresponds to A&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; jax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;loss&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; argnums&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

A &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jnp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;arange&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;astype&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;jnp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
B &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jnp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;arange&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;astype&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;jnp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# autograd output&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;grad_A&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

C &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jnp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;einsum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;ij,jk-&gt;ik&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# gradient of the loss with respect to C&lt;/span&gt;
dL_dC &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jnp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ones_like&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# gradient of the loss with respect to A&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# this is the backpropagation formula we derived&lt;/span&gt;
dL_dA &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jnp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;einsum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;ik,jk-&gt;ij&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dL_dC&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# manually computed output&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dL_dA&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dL_dA &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; grad_A&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Success!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Einsums are a powerful tool for representing and reasoning about linear transformations, and this simple swapping trick makes it very easy to backpropagate through them. I hope you find this informative!&lt;/p&gt;</content:encoded></item><item><title>How Rubik&apos;s Cube blindfolded works</title><link>https://tripplyons.com/blog/rubiks-cube-blindfolded</link><guid isPermaLink="true">https://tripplyons.com/blog/rubiks-cube-blindfolded</guid><description>A visual introduction to blindfolded solving with the Old Pochmann method.</description><pubDate>Tue, 18 Mar 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I made a video that explains the basics of how people solve Rubik’s cubes blindfolded with the Old Pochmann method, using animated visuals.&lt;/p&gt;
&lt;iframe style=&quot;width: 100%; aspect-ratio: 16 / 9; border: 0;&quot; src=&quot;https://www.youtube-nocookie.com/embed/CIk0iWSiYgA&quot; title=&quot;How Rubik&amp;#x27;s Cube Blindfolded Works&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share&quot; referrerpolicy=&quot;strict-origin-when-cross-origin&quot; allowfullscreen&gt;&lt;/iframe&gt;
&lt;p&gt;For more resources, see &lt;a href=&quot;https://blindsolving.com&quot;&gt;blindsolving.com&lt;/a&gt;.&lt;/p&gt;</content:encoded></item><item><title>Rendering fractals by optimizing pixels in JAX</title><link>https://tripplyons.com/blog/jax-fractal-renderer</link><guid isPermaLink="true">https://tripplyons.com/blog/jax-fractal-renderer</guid><description>Rendering fractals with JAX, parallel computation, and pixel optimization—with a CUDA follow-up.</description><pubDate>Sun, 22 Dec 2024 00:00:00 GMT</pubDate><content:encoded>&lt;h2 id=&quot;update-2025-07-25&quot;&gt;Update (2025-07-25)&lt;/h2&gt;
&lt;p&gt;I’ve ported this to CUDA and made it much faster.
Check it out &lt;a href=&quot;https://github.com/tripplyons/cuda-fractal-renderer&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://tripplyons.com/img/blog/jax-fractal-renderer/fractals.png&quot; alt=&quot;Some Fractals I Generated&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;2048&quot; height=&quot;1024&quot;&gt;&lt;/p&gt;
&lt;p&gt;Some Fractals I Generated&lt;/p&gt;
&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I’ve been fascinated by fractals from a young age and how simple rules can generate complex patterns.
In this post, I’ll be going over how I used the machine learning library &lt;a href=&quot;https://github.com/jax-ml/jax&quot;&gt;JAX&lt;/a&gt; to quickly render fractals.&lt;/p&gt;
&lt;h2 id=&quot;why-jax&quot;&gt;Why JAX?&lt;/h2&gt;
&lt;p&gt;JAX makes it easy to write performant parallel code that can run on CPUs, GPUs, TPUs, and other accelerators.
It uses &lt;a href=&quot;https://github.com/openxla/xla&quot;&gt;XLA&lt;/a&gt; to compile the code to run on device, and it has a JIT compiler that can optimize your code’s execution.
Writing JAX code is very similar to writing NumPy code, since JAX includes many of the same APIs.&lt;/p&gt;
&lt;h2 id=&quot;iterated-function-systems&quot;&gt;Iterated function systems&lt;/h2&gt;
&lt;p&gt;The type of fractal I’ll be rendering is an &lt;a href=&quot;https://en.wikipedia.org/wiki/Iterated_function_system&quot;&gt;Iterated Function System&lt;/a&gt;.
An Iterated Function System is a collection of functions that are repeatedly applied to a starting point.
By iteratively applying these functions to the starting point and tracking the result points, we can generate a fractal.
Calculating the result of applying every combination of the functions to the starting point is computationally expensive,
so I chose to use a fixed number of points and randomly choose which function to apply to each point, instead of applying every function to every point.&lt;/p&gt;
&lt;h2 id=&quot;chosen-transformations&quot;&gt;Chosen transformations&lt;/h2&gt;
&lt;p&gt;I used a system of two functions, and chose to use the origin as the starting point. The functions I used were of the form of a composition of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A matrix multiplication&lt;/li&gt;
&lt;li&gt;A vector addition&lt;/li&gt;
&lt;li&gt;A scalar multiplication by a power of the distance from the origin&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In math, this looks like:&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable rowspacing=&quot;0.16em&quot; columnalign=&quot;center&quot; columnspacing=&quot;1em&quot;&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;msup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo mathvariant=&quot;normal&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;′&lt;/mo&gt;&lt;/msup&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;msup&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mo mathvariant=&quot;normal&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;′&lt;/mo&gt;&lt;/msup&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;msup&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable rowspacing=&quot;0.16em&quot; columnalign=&quot;center center&quot; columnspacing=&quot;1em&quot;&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable rowspacing=&quot;0.16em&quot; columnalign=&quot;center&quot; columnspacing=&quot;1em&quot;&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable rowspacing=&quot;0.16em&quot; columnalign=&quot;center&quot; columnspacing=&quot;1em&quot;&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\begin{bmatrix}
x&apos; \\\\
y&apos;
\end{bmatrix}
=
\frac{1}{|\ldots|^p}
\left(
\begin{bmatrix}
a &amp;#x26; b \\\\
d &amp;#x26; e
\end{bmatrix}
\begin{bmatrix}
x \\\\
y
\end{bmatrix}
+
\begin{bmatrix}
c \\\\
f
\end{bmatrix}
\right)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;h2 id=&quot;applying-transformations&quot;&gt;Applying transformations&lt;/h2&gt;
&lt;p&gt;In JAX, applying this class of transformation to a list of points looks like this:&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apply_transform&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; settings&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; d&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; power &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; settings

    x_prime &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; c
    y_prime &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; d &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; e &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; f

    scale_squared &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x_prime &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; y_prime &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# divide by 2 for square root&lt;/span&gt;
    x_prime &lt;span class=&quot;token operator&quot;&gt;/=&lt;/span&gt; scale_squared &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;power &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    y_prime &lt;span class=&quot;token operator&quot;&gt;/=&lt;/span&gt; scale_squared &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;power &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; x_prime&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y_prime
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;updating-points&quot;&gt;Updating points&lt;/h2&gt;
&lt;p&gt;The following function randomly applies one of the transforms to the points to get the next set of points.
This is similar to the &lt;a href=&quot;https://en.wikipedia.org/wiki/Chaos_game&quot;&gt;Chaos Game&lt;/a&gt; method.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; jax
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; jax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; jnp

dtype &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jnp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; settings&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rng&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    rate&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; settings_1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; settings_2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; settings

    &lt;span class=&quot;token comment&quot;&gt;# apply the first transform to the points&lt;/span&gt;
    x_prime_1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y_prime_1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; apply_transform&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; settings_1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# apply the second transform to the points&lt;/span&gt;
    x_prime_2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y_prime_2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; apply_transform&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; settings_2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# randomly choose one of the transforms (chaos game)&lt;/span&gt;
    mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;random&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;uniform&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;rng&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; shape&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; minval&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; maxval&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;dtype&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mask &lt;span class=&quot;token operator&quot;&gt;&amp;#x3C;&lt;/span&gt; rate&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# choose the first transform where the mask is 1&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# and the second transform where the mask is 0&lt;/span&gt;
    x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x_prime_1 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; mask &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; x_prime_2 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; mask&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; y_prime_1 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; mask &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; y_prime_2 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; mask&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;parallelism&quot;&gt;Parallelism&lt;/h2&gt;
&lt;p&gt;It’s important to note that the code transforms many points at once.
This allows the program to generate images more quickly on most modern hardware.
The points will all start at the same position, but will diverge as different transforms are randomly chosen.&lt;/p&gt;
&lt;h2 id=&quot;optimizing-pixels&quot;&gt;Optimizing pixels&lt;/h2&gt;
&lt;p&gt;Let’s refer to the rate at which the points appear within a pixel as the “density” at that pixel.
To generate an image, we can treat lower density pixels as black and higher density pixels as white.
To go from transforming points to generating an image of a fractal, we can start with a black image (zero density) and iteratively update the pixels based on the current set of points.&lt;/p&gt;
&lt;p&gt;We can use an optimizer designed for gradient descent to optimize the pixels.
Even though we are not using gradient descent (we have no loss function), we can still use the same optimizer while treating the gradient as an update to the density, rather than a set of derivatives of a loss function.
We can set the “gradient” to 1 at the current locations of the points and 0 elsewhere to maximize the density at the current locations.
I used the &lt;a href=&quot;https://github.com/evanatyourservice/psgd_jax&quot;&gt;PSGD&lt;/a&gt; optimizer to optimize the pixels, since it gave the best results for my use case.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; tqdm
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; optax
&lt;span class=&quot;token comment&quot;&gt;# the optimizer that I found to work the best&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; psgd_jax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;affine &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; scale_by_affine

index_dtype &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jnp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;int32

&lt;span class=&quot;token comment&quot;&gt;# density is the vector of the density values at each point&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# it is initialized to zeros&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# it is updated where the current points are&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;optimize&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;density&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x_res&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y_res&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; settings&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; iterations&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bounds&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rng&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    min_x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; max_x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; min_y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; max_y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; bounds

    &lt;span class=&quot;token comment&quot;&gt;# use a warmup schedule to start the optimization at a lower learning rate&lt;/span&gt;
    schedule &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; optax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;schedules&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;warmup_constant_schedule&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; iterations &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    optimizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; optax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;chain&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        scale_by_affine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# optimizer&lt;/span&gt;
        optax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;scale_by_schedule&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;schedule&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# learning rate schedule&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    opt_state &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; optimizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;init&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;density&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# start at the origin&lt;/span&gt;
    x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jnp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;dtype&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jnp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;dtype&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# calculate how large each pixel is&lt;/span&gt;
    dx &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;max_x &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; min_x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x_res &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    dy &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;max_y &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; min_y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y_res &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@jax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;jit&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;density&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; opt_state&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rng&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        update_rng&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rng &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;random&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;rng&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# update the points&lt;/span&gt;
        x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; update&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; settings&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; update_rng&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# find the nearest pixel to the current points&lt;/span&gt;
        x_index &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jnp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; min_x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; dx&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;astype&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;index_dtype&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        y_index &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jnp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; min_y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; dy&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;astype&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;index_dtype&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# go to an invalid index after the end of our vector if the coordinate is negative or too large&lt;/span&gt;
        x_index &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x_index &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;x_index &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; x_res&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x_index &lt;span class=&quot;token operator&quot;&gt;&amp;#x3C;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x_index &lt;span class=&quot;token operator&quot;&gt;&gt;=&lt;/span&gt; x_res&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        y_index &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; y_index &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;y_index &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; y_res &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; x_res&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y_index &lt;span class=&quot;token operator&quot;&gt;&amp;#x3C;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y_index &lt;span class=&quot;token operator&quot;&gt;&gt;=&lt;/span&gt; y_res&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# set the gradient to 0 by default&lt;/span&gt;
        grad &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jnp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x_res &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; y_res&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;dtype&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# set the gradient to 1 at the current locations&lt;/span&gt;
        grad &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; grad&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;at&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;x_index &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; y_res &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; y_index&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# normalize the gradient to help the optimizer converge faster&lt;/span&gt;
        grad &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;standardize&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;grad&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        updates&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; opt_state &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; optimizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;update&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;grad&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; opt_state&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; density&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        density &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; optax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_updates&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;density&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; updates&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; density&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; opt_state&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rng

    &lt;span class=&quot;token comment&quot;&gt;# run the optimization for the specified number of iterations&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; _ &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; tqdm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;iterations&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        density&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; opt_state&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rng &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; step&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;density&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; opt_state&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rng&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; density
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;results&quot;&gt;Results&lt;/h2&gt;
&lt;p&gt;The optimization ran very quickly, at over 1000 optimization steps per second on a T4 GPU
with a batch size of 1024 and a resolution of 1024x1024. Since I ran 10000 steps per image,
it took less than 10 seconds to generate each image.&lt;/p&gt;
&lt;p&gt;I found a somewhat good range of transformation parameters to use that are included in the code,
and I would recommend trying to change them to see what kind of results you get.&lt;/p&gt;
&lt;p&gt;My favorite image of the 25 images I generated is below:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://tripplyons.com/img/blog/jax-fractal-renderer/favorite.png&quot; alt=&quot;My Favorite Image&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1024&quot; height=&quot;1024&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I think this is a pretty cool way to render fractals and a cool use case for JAX.
I’m excited to see other ways people use JAX that I haven’t thought of.
I hope you find this useful or inspiring!&lt;/p&gt;
&lt;h2 id=&quot;source-code&quot;&gt;Source code&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/tripplyons/jax-fractal-renderer&quot;&gt;GitHub Repository&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://colab.research.google.com/github/tripplyons/jax-fractal-renderer/blob/main/jax-fractal-renderer.ipynb&quot;&gt;Google Colab Notebook&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded></item><item><title>Train a text summarizer in 5 minutes</title><link>https://tripplyons.com/blog/text-summarizer</link><guid isPermaLink="true">https://tripplyons.com/blog/text-summarizer</guid><description>Fine-tune Flan-T5 on XSum with adapter-transformers to build a text summarizer.</description><pubDate>Sat, 17 Dec 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Have you ever wanted to fine-tune a large language model,
but didn’t have the time or resources to train it from scratch?
In this post, I’ll show you how to use adapter-transformers
to fine-tune Flan-T5 on the XSum dataset in five minutes.
I’ll also show you how to use the resulting model to generate summaries for any text.&lt;/p&gt;
&lt;h2 id=&quot;what-is-fine-tuning&quot;&gt;What is fine-tuning?&lt;/h2&gt;
&lt;p&gt;Fine-tuning is the process of training a pre-trained model on a new dataset.
This usually takes less time than training a model from scratch,
but it can still take a long time.&lt;/p&gt;
&lt;h2 id=&quot;what-is-adapter-transformers&quot;&gt;What is adapter-transformers?&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://adapterhub.ml/&quot;&gt;adapter-transformers&lt;/a&gt; is a library for fine-tuning transformers,
based on HuggingFace’s &lt;a href=&quot;https://huggingface.co/docs/transformers/index&quot;&gt;transformers&lt;/a&gt; library.
It has a number of new features that make it easier to fine-tune transformers,
such as LoRA (Low Rank Adaptation), which allow you to reduce the amount of parameters
that need to be trained.&lt;/p&gt;
&lt;h2 id=&quot;what-is-lora&quot;&gt;What is LoRA?&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;https://tripplyons.com/img/blog/lora.png&quot; alt=&quot;Diagram of LoRA&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1280&quot; height=&quot;720&quot;&gt;&lt;/p&gt;
&lt;p&gt;LoRA works by training two small matrices
that are multiplied together to produce a large matrix.
This large matrix is then added to the pre-trained model’s weights,
usually in the attention layers. Adding the matrices
is equivalent to adding the results of the matrix multiplication,
which is done in training.&lt;/p&gt;
&lt;h2 id=&quot;the-flan-t5-model&quot;&gt;The Flan-T5 model&lt;/h2&gt;
&lt;p&gt;Flan-T5 is a series of models by Google that are based on the T5 architecture.
They are text-to-text transformers that can be used for a variety of tasks,
making them a great starting point for fine-tuning.&lt;/p&gt;
&lt;p&gt;In this post, I’ll be using &lt;a href=&quot;https://huggingface.co/google/flan-t5-base&quot;&gt;flan-t5-base&lt;/a&gt;,
which is one of the smaller models in the series.&lt;/p&gt;
&lt;h2 id=&quot;the-xsum-dataset&quot;&gt;The XSum dataset&lt;/h2&gt;
&lt;p&gt;XSum stands for Extreme Summarization and is a dataset of documents and summaries.&lt;/p&gt;
&lt;p&gt;In this post, I’ll be using a small subset of the XSum dataset,
which contains 1,000 documents and their summaries.&lt;/p&gt;
&lt;h2 id=&quot;environment-setup&quot;&gt;Environment setup&lt;/h2&gt;
&lt;p&gt;I’ll be using &lt;a href=&quot;https://colab.research.google.com/&quot;&gt;Google Colab&lt;/a&gt; for this post,
but you can also run it locally if you have a GPU and the necessary libraries installed.&lt;/p&gt;
&lt;p&gt;To install the libraries that aren’t installed in Google Colab, you’ll have to run the following commands:&lt;/p&gt;
&lt;pre class=&quot;language-text&quot; data-language=&quot;text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;pip install -U adapter-transformers sentencepiece
pip install datasets
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;tokenizing-the-dataset&quot;&gt;Tokenizing the dataset&lt;/h2&gt;
&lt;p&gt;In order to use the Flan-T5 model, we’ll have to convert the documents and summaries
into tokens that the model can understand.&lt;/p&gt;
&lt;p&gt;To do this, we’ll use the AutoTokenizer class from the transformers library.
We’ll also use a prefix before the documents, which is used by the model to determine
what task it should perform. In this case, the prefix is “summarize: ”. We have to do
this because this is the format the model expects.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; AutoTokenizer

&lt;span class=&quot;token comment&quot;&gt;# the base model that we&apos;ll be using&lt;/span&gt;
base_model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;google/flan-t5-base&quot;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# the tokenizer that we&apos;ll be using&lt;/span&gt;
tokenizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoTokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;base_model&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# the prefix that we&apos;ll be using&lt;/span&gt;
prefix &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;summarize: &apos;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# tokenize the dataset&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;encode_batch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;examples&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# the name of the input column&lt;/span&gt;
    text_column &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;document&apos;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# the name of the target column&lt;/span&gt;
    summary_column &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;summary&apos;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# used to format the tokens&lt;/span&gt;
    padding &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;max_length&quot;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# convert to lists of strings&lt;/span&gt;
    inputs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; targets &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;examples&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;text_column&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; examples&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;text_column&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; examples&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;summary_column&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            inputs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;examples&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;text_column&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            targets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;examples&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;summary_column&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# add prefix to inputs&lt;/span&gt;
    inputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;prefix &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; inp &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; inp &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; inputs&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# finally we can tokenize the inputs and targets&lt;/span&gt;
    model_inputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; max_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; padding&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;padding&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; truncation&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    labels &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;targets&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; max_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; padding&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;padding&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; truncation&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# rename to labels for training&lt;/span&gt;
    model_inputs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;labels&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; labels&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;input_ids&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; model_inputs
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;loading-the-dataset&quot;&gt;Loading the dataset&lt;/h2&gt;
&lt;p&gt;We’ll be using the datasets library to load the dataset,
and we will load a subset of the dataset from each split.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; datasets &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; load_dataset

&lt;span class=&quot;token comment&quot;&gt;# load the dataset&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;load_split&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;split_name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; max_items&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# load the split&lt;/span&gt;
    dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; load_dataset&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;xsum&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;split_name&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# only use the first max_items items&lt;/span&gt;
    dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; _&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; idx&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; idx &lt;span class=&quot;token operator&quot;&gt;&amp;#x3C;&lt;/span&gt; max_items&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; with_indices&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# tokenize the dataset&lt;/span&gt;
    dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        encode_batch&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        batched&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        remove_columns&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;column_names&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        desc&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Running tokenizer on &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; split_name &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; dataset&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# set the format to torch&lt;/span&gt;
    dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;set_format&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;torch&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; columns&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;input_ids&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;labels&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; dataset
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;preparing-the-model&quot;&gt;Preparing the model&lt;/h2&gt;
&lt;p&gt;Now that we have the dataset, we can prepare the model for training.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; AutoModelForSeq2SeqLM
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;adapters &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; LoRAConfig
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np

&lt;span class=&quot;token comment&quot;&gt;# start with the pretrained base model&lt;/span&gt;
model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoModelForSeq2SeqLM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    base_model
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# set the parameters for LoRA&lt;/span&gt;
config &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; LoRAConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    r&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    alpha&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# use it on all of the layers&lt;/span&gt;
    intermediate_lora&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    output_lora&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# make a new adapter for the XSum dataset&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add_adapter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;xsum&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; config&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;config&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# enable the adapter for training&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train_adapter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;xsum&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;set_active_adapters&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;xsum&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;preparing-the-trainer&quot;&gt;Preparing the trainer&lt;/h2&gt;
&lt;p&gt;Now that we have the model, we can prepare the trainer.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; AdapterTrainer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; TrainerCallback

&lt;span class=&quot;token comment&quot;&gt;# small batch size to fit in memory&lt;/span&gt;
batch_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;

training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    learning_rate&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3e-4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    num_train_epochs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    per_device_train_batch_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    per_device_eval_batch_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    logging_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    output_dir&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;./training_output&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    overwrite_output_dir&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    remove_unused_columns&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# create the trainer&lt;/span&gt;
trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AdapterTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    args&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;training_args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    tokenizer&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# load the dataset&lt;/span&gt;
    train_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;load_split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;train&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    eval_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;load_split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;test&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;training-the-model&quot;&gt;Training the model&lt;/h2&gt;
&lt;p&gt;Now that we have the trainer, we can train the model.
This will take a while, especially depending on your hardware.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;evaluating-the-model&quot;&gt;Evaluating the model&lt;/h2&gt;
&lt;p&gt;Now that we have the trained model, we can evaluate its performance on the test split.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;evaluate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;merging-the-adapters&quot;&gt;Merging the adapters&lt;/h2&gt;
&lt;p&gt;Like I mentioned earlier, we will need to add the matrices.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# merge the adapter with the model&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# this will add the adapter weight matrices to the model weight matrices&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;merge_adapter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;xsum&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;generating-summaries&quot;&gt;Generating summaries&lt;/h2&gt;
&lt;p&gt;We will use the validation split of the dataset and compare our summaries
with the actual summaries.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;num_validation &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;

validation_dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; load_split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;validation&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_validation&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;num_validation&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# load the input and label&lt;/span&gt;
    input_ids &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; validation_dataset&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;input_ids&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;unsqueeze&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    label_ids &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; validation_dataset&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;labels&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;unsqueeze&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# use the model to generate the output&lt;/span&gt;
    output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;generate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input_ids&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; max_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# convert the tokens to text&lt;/span&gt;
    input_text &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input_ids&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; skip_special_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    output_text &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; skip_special_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    label_text &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;label_ids&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; skip_special_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Input:&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; input_text&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Output:&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; output_text&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Label:&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; label_text&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;---&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;saving-the-model&quot;&gt;Saving the model&lt;/h2&gt;
&lt;p&gt;Now that we have the trained model, we can save it. Mine is saved to &lt;a href=&quot;https://huggingface.co/tripplyons/flan-t5-base-xsum&quot;&gt;tripplyons/flan-t5-base-xsum&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot; data-language=&quot;python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# login to upload the model&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; huggingface_hub &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; login
login&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; huggingface_hub &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; HfApi
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; torch
api &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; HfApi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state_dict&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;pytorch_model.bin&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

api&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;upload_file&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    path_or_fileobj&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;pytorch_model.bin&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    path_in_repo&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;pytorch_model.bin&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# replace with your own username in order to upload&lt;/span&gt;
    repo_id&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tripplyons/flan-t5-base-xsum&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    repo_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Hopefully this tutorial was helpful in showing how to quickly fine-tune a model
for a new task using adapters. My source code for this tutorial can be found
on &lt;a href=&quot;https://colab.research.google.com/drive/1ieDHXRIQO98qhwycCpKYqg4OKRs4L8Yf&quot;&gt;Google Colab&lt;/a&gt;.&lt;/p&gt;</content:encoded></item><item><title>Nextcloud bug bounty report (CVE-2020-8155)</title><link>https://tripplyons.com/blog/nextcloud-bug-bounty</link><guid isPermaLink="true">https://tripplyons.com/blog/nextcloud-bug-bounty</guid><description>The discovery and disclosure of a Nextcloud cross-site scripting vulnerability.</description><pubDate>Thu, 04 Jun 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;After listening to an episode of &lt;a href=&quot;https://darknetdiaries.com/&quot;&gt;Darknet Diaries&lt;/a&gt; about bug bounty programs, I was inspired to make some money finding vulnerabilities in websites. Through &lt;a href=&quot;https://www.hackerone.com/&quot;&gt;HackerOne&lt;/a&gt;, I discovered &lt;a href=&quot;https://hackerone.com/nextcloud&quot;&gt;Nextcloud’s bug bounty program&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;how-i-found-the-vulnerability&quot;&gt;How I found the vulnerability&lt;/h2&gt;
&lt;p&gt;While looking at the list of in-scope targets, I saw that one was a PDF viewer, and some PDF viewers are vulnerable to XSS attacks.
After seeing that they used &lt;a href=&quot;https://mozilla.github.io/pdf.js/&quot;&gt;pdf.js&lt;/a&gt;, I searched for vulnerabilities that affected the version they used.
I found out that the version they used was vulnerable to &lt;a href=&quot;https://bugzilla.mozilla.org/show_bug.cgi?id=1452075&quot;&gt;CVE-2018-5158&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-report-process&quot;&gt;The report process&lt;/h2&gt;
&lt;p&gt;Although the report was only around 150 words, I spent a few hours confirming the exploit and writing it. I link to the report at the bottom of this post.&lt;/p&gt;
&lt;h2 id=&quot;what-happened&quot;&gt;What happened&lt;/h2&gt;
&lt;p&gt;Nextcloud quickly responded, fixed the issue, and submitted a &lt;a href=&quot;https://en.wikipedia.org/wiki/Common_Vulnerabilities_and_Exposures&quot;&gt;CVE&lt;/a&gt;.
They gave me $100, and the vulnerability was given the CVE ID, &lt;a href=&quot;https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-8155&quot;&gt;CVE-2020-8155&lt;/a&gt;.
Overall, I am pleased with how they communicated and handled the process.&lt;/p&gt;
&lt;h2 id=&quot;links&quot;&gt;Links&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://nextcloud.com/security/advisory/?id=NC-SA-2020-019&quot;&gt;Nextcloud’s Advisory&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://hackerone.com/reports/819863&quot;&gt;HackerOne Report&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-8155&quot;&gt;CVE-2020-8155 (CVE Database)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://nvd.nist.gov/vuln/detail/CVE-2020-8155&quot;&gt;CVE-2020-8155 (NIST.gov)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded></item></channel></rss>