Post

The LLM rabbit hole

The LLM rabbit hole

a small peek through the entire math of llms, hallucination detection with entropy, new topologies for reasoning models, insights

A bit of context here -

This won’t be like some of my other projects.

This is just me getting sucked into an absolute rabbit hole. A hole so deep that it got me researching and rediscovering papers in 2026 and getting to the cutting edge of the LLM optimization race.

For context, let’s round up what all I went through (in no particular order since I’m ranting anyway)

  • Try to discover brand new topologies for making a conjoined reasoning network on said “decentralized” network
  • Reading, absorbing the entire math behind LLMs including stuff like transformers, attention, embedding and post mortem of a deceased but still pure “meta llama 3.2-B model”
  • Rediscovering pipeline-parallelism (distribution of compute in layers)
  • Rediscovering reasoning-parallelism (spawning many branches and selecting the best one)
  • Rediscovering the collapse of rank in context matrices in the hallucination of LLMs
  • Trying to send raw hidden activation INT8 quantized tensors over TCP
  • Asynchronous speculative branching in reasoning using Shannon entropy
  • Backtracking and critic - explorer - architect based reward model
  • Attempting to break the LLMs repeatedly forcing hallucinogens onto them
  • Trying to detect LLM hallucinations with spectral entropy of context matrices
  • Not once, twice or thrice but getting kicked from my Colab account 4 times in a row, every single day (wasn’t fun)

  • Applying Von Neumann entropy, Eigen decomposition, Shannon entropy and a some other mathematical tools and visualization
  • New insights like basins and balls and me being extremely sleep deprived (explain more later)

How this all started

So for the past few weeks I’ve been absolutely wracking my brain trying to build a decentralized system to make a reasoning network out of smaller LLM models.

So my general idea 2 weeks ago was “hey if LLMs are so smart why don’t we just make more of them talk to each other they can reason better that way”. Simple thinking. Those were simple times.

The Wisdom of Crowds is actually a very real phenomenon - When you put a lot of experts into a room, of different backgrounds and fields they’re more likely to produce an output that comes down to the global optimal solution.

I remember watching this one lecture by Robert Sapolsky - he was giving a lecture at Stanford in which he raised an example so good i was in awe when i first heard it

In a county fair, a contest has a lot of people participating to predict the weight of an ox. Now everyone tries their best to guess the weight, but no one could do it, and the contest requires you to be within 1 kilogram of the actual answer. Our scientist in question who did the study, went around collected the chits of paper of the guesses. Turns out.. when he averaged them out, it landed short of 100 g of the real answer.

The question is, can I make something that can knock out newly generated problems by reasoning with itself?

Cheat Sheet to make ur own reasoning engine at home!1!!!!
  • Parameters: You need a good knowledge base. Bigger the model, better knowledge capacity it has, and generally you can fine tune smaller models if you’re into squeezing every last of your parameters down to absolute 0.

  • Attention decay: Next you need to make sure your model doesn’t spiral out of control. Does it have enough temporal space in it’s brain to process a full stream of thought? This actually depends on 2 factors, the number of layers of the neural net (which has been proven by papers that it can reason in between layers) and a multi head attention span.

  • Thought patterns: Based on a real brain, your approaches to problem solving or constructing ideas have these 3 steps

    • Identify subtasks (Architect)
    • Explore viable solutions to a subtask, or think up previous solutions (Explore)
    • Eliminate impossible solutions (Critic)

    Using some amount of context injection you can make the LLM think deeper just making it reason out the plan for the response it’s going to generate (models like DeepSeek v3 relies on this for heavier prompting.)

Onward to my first approach

Dynamic Entry Point Routing based on Semantic Continuity

So what happened here?

My reasoning was very simple -

Be me

Have a bunch of shards (mini servers/computers)

Prompt comes into one shard

Shard starts processing for a few tokens (words) then recognizes the paragraph has converged and finished it’s meaning

Shard stops processing right before it’s final layer at that moment

Shard transfers the same context in raw vectors to 2 other shard buddies

Asynchronously now, 2 shards are going to try go down different branches of reasoning

Rinse and repeat till you have trees of meaning

At the very end, collect all the meaningful branches, compile them, expect result, boom.

image-20260808005205801

My main issue was that I didn’t really understand the concept of an LLM? or KV cache, or Attention matrices, or context in general. There were so many things wrong with this idea, I literally couldn’t even fathom the fact that it came out of me.

Know your enemy and know thyself, and you will not fear the result of a thousand battles. - Sun Tzu

I didn’t actually know my enemy. I was so used to doing Feed Forward networks from my years building CNNs and Dense networks that i legitimately didn’t know the conceptual understanding that went into an LLM.

If it was that easy to displace pieces of a black box across the internet and put it back together, we’d be much further into LLM research by now. But, I savored some parts of this experiment -

https://github.com/null-Exception1/dLLM

The thing is i was so banked on the idea that it was going to work that I systemized it without a clue in my head that the core concept was not worth adding all this for

For example

  • State inversion - Before hitting the final softmax layer (the one that makes numbers turn to text), you transfer the numbers directly to another shard, this makes it so that there’s no text serialization which is pretty computationally intensive. So basically, telepathy for computers.

  • Consistent Hashing Ring - How does a shard know which shard to contact? How does a shard know where different contexts are stored? Where the next one went? A consistent hashing ring solves the problem by keeping a deterministic algorithm that maps the context’s subtask to a specific shard, and this way if shards are suddenly interrupted or switched out there’s no real restructuring except a minor multicast to all the shards that “hey someone’s gone now”, there will be minimal communication between all the shards.
  • Decentralized Self-Seeding- If you decide the operational boundaries you’re working with for a prompt you can easily map out the exact shards you’d need

  • 8 Bit Uniform Quantization- There’s actually 2 parts to this and it’s quite fantano -
    • Basically, when we’re transferring hidden activation states over the network, we reduce their size so an outgoing stream costs us less
    • But the thing is, we can decrease our outgoing stream by up 75%. How’s that possible? Well according to a research paper done by some very smart folks, an LLM doesn’t actually need all the hidden state vectors. It generally functions on the upper half of vectors which have big magnitudes and ignores the small ones because they cause very minimal changes in output. If an LLM can pick up the nearly the same context with only 25% of the data, that’s every bit more exciting to build this. This was honestly the entire reason i was trying to build the project.
    • There were some other things like min-max scanning, scale factor calculation etc. but we have lots more to cover right now so nah
    • This also leads to a problem called Quantization Drift so buckle up
  • Speculative Entropy Branching- If you thought this was going to show up, you’re damn right. Based on some research papers it’s actually likely to predict the health of text generation and convergence of ideas and topics towards endings or new beginnings, so i was looking forward to building this - but alas i couldn’t get to it in time.
  • Unrelated but- my laptop was struggling so i could only simulate the working of gRPC shards, I had a third party tunnel to Colab which functioned for multiple shards simultaneously.

Awakening

So this was when I realized,

Yeah I’m kind of outmatched on this one idea. I’ll spend a full day just diving deep into the math so i can actually find if there’s something I can pull out of this.

Right so let’s dive a little into our LLM math real quick. It’s not a full class and you can skip this if you want to.

Like really quickly because I’m not running a class here -


1. Input

Before any math happens, text is broken into tokens (words or sub-words).


2. The Scaled Dot-Product Attention

The core of the Transformer architecture is the attention mechanism.

For a single attention head, the hidden states matrix \(H\) is projected into three distinct spaces — Queries (\(Q\)), Keys (\(K\)), and Values (\(V\)). Using specially trained weights for a model (essentially the brain) \(W_Q\), \(W_K\), and \(W_V\), we perform this operation:

\[Q = HW_{Q}\] \[K = HW_{K}\] \[V = HW_{V}\]

Basically, Queries and Keys vectors are going to be the main ways to read prompt text and assign context to specific tokens in the prompt text. So we use them to get our attention matrix which is a combination of both; essentially we are both querying and reading what a prompt means.

\[A = \text{softmax}\left(\frac{QK^{T}}{\sqrt{d_{k}}}\right)\]
  • Softmax: Applied row-wise, turning scores into probabilities that sum up to 1.

The final output of the attention mechanism scales the Value matrix by these probabilities:


3. Position-Wise Feed-Forward Network (FFN)

This is where the brain starts doing its thing.

In a network, a general expression for feed forwarding looks like:

\[\text{Output} = \text{Activation}(W \cdot X + b)\]

Where \(W\) is weight and \(b\) is bias.

This is basically a linear polynomial which explains why neural networks are called polynomial regressions sometimes, as a large amount of combining linear polynomials with activation functions lead to something called a Piecewise Linear Network. This is standard knowledge for anyone using any form of neural networks btw.

\[\text{FFN}(H) = \text{Activation}(HW_{1} + b_{1})W_{2} + b_{2}\]

Where:

  • \(W_1 \in \mathbb{R}^{d \times d_{ff}}\) (where \(d_{ff}\) is typically \(4 \times d\))
  • \[W_2 \in \mathbb{R}^{d_{ff} \times d}\]
  • \(b_1, b_2\) are bias vectors.

4. Residual Connections

Deep networks need to carry forward some data to the output layers, otherwise they can’t actually train or do meaningful stuff.

So we do a residual norm and pass some of the older layer data to the newer layer (as a form of assurance):

\[H_{\text{mid}} = \text{LayerNorm}(H + \text{MultiHead}(H))\] \[H_{\text{out}} = \text{LayerNorm}(H_{\text{mid}} + \text{FFN}(H_{\text{mid}}))\]

5. Output

\[\text{logits} = h_{\text{last}}E^{T} \in \mathbb{R}^{1 \times V}\]

Finally, a softmax function converts these logits into a probability distribution over the entire vocabulary:

\[P(\text{next token}) = \text{softmax}(\text{logits})\]

As any AI researcher or smartass can guess, I’m massively oversimplifying and had to cut out a ton of shit for the sake of my blog. If you do want to check out the missing steps, they’re very cool optimizations written by some very smart people like MHA (multi-head attention) or RoPE for positional embedding.

The main thing I want you to focus on is this:

\[A = \text{softmax}\left(\frac{QK^{T}}{\sqrt{d_{k}}}\right)\]

Attention is a very very fragile thing - not just LLM computing, but in life in general. If attention gets spliced or distraught, many devastating things happen. For example, sleeping through your entire chemistry class because you weren’t paying attention to a new schedule given by the professor.

The problem with most LLMs is that attention is a massive matrix that grows quadratically with time.

This is why my previous approach didn’t work - if i was transferring hidden state activations, I’d have to transfer my attention matrix and K-V cache along with it. And in a tree with multiple branches, by the end of that tree you’d be sending some 4000x4000 attention matrix across a network which can barely hit 0.5 Mbps on a good day.

LLMs do not simply gain the power to know what they’re talking about, they store it in K-V caches and Attention matrices.

If you’re wondering what a K-V Cache is, it’s basically a Key to Value Cache that makes sure that the LLM knows over a period of time what we’re talking about and which stuff. Basically assign meanings to words, so it can correlate with your previous prompts.

I thought of it as a checkpoint game, with similar models you can resume the same checkpoint somewhere else if you just carry or clone the data somewhere else. I was wrong.

Pipeline Parallelism

So you know how we were trying to compute between layers right?

The entire industry collectively has done it in some way or another - By basically distributing compute you save on bottlenecks and whatnot, so you put different layers of the LLM on each machine, that way the compute is evenly distributed. The only difference is that they’re not on consumer grade hardware so they can do whatever they want by sending raw tensors over.

So many damn papers were written on this i couldn’t even bother checking. My ideas were encroaching this territory so hard that i couldn’t think of anything else.

My next approach

Fine we can’t transfer tensors, we can try to inject text right? Like at the end of the day, text is what LLMs understand, it’s easy to send over a network, it functions as it’s own context that can be recomputed - wait i’ve got it!

Asynchronous Token Interruption Reasoning Engine

Tell me you’re stupid without telling me you’re stupid

Listen, it’s been done before, I don’t want to hear it. It inspired a little bit of this -

https://github.com/null-Exception1/contextinjection

This is unrelated though and only a mini side project

The idea was simple, have 2 layers

Prompt gets distributed by an architect node to different topic meshes

Each topic mesh is technically a collection or cluster of nodes

Each topic mesh consists of an architect, explorer, critic

Right so each node is it’s own model that runs a hyper specialized version of it’s role

The architect is a model which creates subtasks for the task

The explorer rapidly generates streams of new ideas and thoughts

The critic critiques and removes the non-worthy suggestions

Some more details i missed

  • A critic would constantly read a UDP stream from the explorer, when it detects a faulty context or a faulty assumption from the explorer it sends a terminate and retry signal
  • Explorer would then retry the same context with a different appendage or a new meaning
  • Architect would maybe be recalled into doing a separate subtask

These topic meshes operate on a chalkboard together, they try to build something out of nothing but pure ingenuity. Motivating stuff, really.

Understood? Now all the topic meshes finally conclude on the paragraphs they want to send back for their respective tasks they were given or topics if you want to call them. They get sent back, a node fully aligns and combines them, easy peasy.

So naturally instead of building the entire system from scratch, i tried to test the core concept to see if it actually works or not.

This led me to a very interesting insight (i.e i failed so miserably i gained enlightenment)-

  • Models extremely bad at pivoting - Even if a critic critiques their work, it’s highly unlikely they’d do anything about it and go with it repeating the bad loop repeatedly.
  • Then I got sleep deprived and kind of face first found myself discovering an insight -
  • They actually revolve around in a basin of a pre context they write beforehand - it’s sort of like a “die on that hill” situation for a deterministic LLM because of their determinism they’re unable to guide themselves out of a path that eventually leads them to the ultimate ground truth on which that factual conversation would end, which means that they follow the idea blindly, full of intuition and without a care in the world.
  • What is a Basin? - Well a basin is a curvature around which if an LLM operates, they would ride the slope down to the minimum of that basin. It’s important to note that there’s mini basins within basins, and these are implicit, they aren’t well defined or well perceived by anyone.

  • This is when i realized that if you drop a ball into multiple basins, you could gain better insight on topics and it would be much faster to sift through them if you speed forward to the truth

image-20260808023434666

So yeah me writing this down at 1 am then getting up to do classes at 8 am - fantastic idea.

Reasoning based parallelism

Outdone yet again. What a shame.

Large companies generally start by making a model produce 10 different streams to a single prompt, out of which the highest probability one (the most confident one) is then given priority and proceeded with.

This is essentially the same idea as they’re throwing a ball into many basins and then picking the best basin. Wasteful, but it works. I discovered that they do this AFTER my epiphany occurred which means I’m basic and boring :(

The idea behind trying to continue different streams of thought still remained though, i was trying to think of a better solution to wasting those other 9 basins, leave no information behind type stuff. Any information left behind is wasteful and on consumer hardware especially, where we need all the optimizations we can get.

Backtracking

If you can’t really convince an AI to change it’s ways, you just change it’s pre context. It’s the same thing as moving basins, except we’re operating on the mini-basin depth too

If you can somehow operate around mini basins by backtracking and making the LLM randomly step into other basins, you could technically guide it to a better result that most would’ve just handing it a random path and it getting stuck on random mini basins.

Now unfortunately i hadn’t exactly thought of this idea yet. This was coming though- There was just one final piece we needed, which was a detection function which gave away the timing at which we could sense that the LLM was underconfident or overconfident. The detection function was just spectral entropy analysis on the raw attention matrix at the time - which failed miserably.

Now the problem with the detector is that it can become a VERY harsh critic if you don’t understand thresholding correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
Starting Reasoning Loop Pipeline with Clean Cache Re-Evaluation...
-----------------------------------------------------------------------------------------------
Token 01: ' Step          ' | Radar Status: CLEAN
Token 02: ' 1             ' | Radar Status: CLEAN
Token 03: ':              ' | Radar Status: CLEAN
Token 04: ' verify        ' | Radar Status: CLEAN

[RADAR TRIP] LOGIC TRAJECTORY FREEZE DETECTED IN TRANSFORMER STRATA!
[SURGICAL STEP] Initiating Split-Fork Cache Invalidation Protocol...
   -> Dropped last 15 tokens from active text pipeline.
   -> Injected Clean Cognitive Detour Directive.
   -> [CACHE INVALIDATION] Dropping past key-values. Forcing a clean context matrix re-evaluation...

Token 05: '               ' | Radar Status: CLEAN
Token 06: '
              ' | Radar Status: CLEAN
Token 07: 'Solution       ' | Radar Status: CLEAN
Token 08: ':              ' | Radar Status: CLEAN

[RADAR TRIP] LOGIC TRAJECTORY FREEZE DETECTED IN TRANSFORMER STRATA!
[SURGICAL STEP] Initiating Split-Fork Cache Invalidation Protocol...
   -> Dropped last 15 tokens from active text pipeline.
   -> Injected Clean Cognitive Detour Directive.
   -> [CACHE INVALIDATION] Dropping past key-values. Forcing a clean context matrix re-evaluation...

Token 09: '               ' | Radar Status: CLEAN
Token 10: '
              ' | Radar Status: CLEAN
Token 11: 'Solution       ' | Radar Status: CLEAN
Token 12: ':              ' | Radar Status: CLEAN

[RADAR TRIP] LOGIC TRAJECTORY FREEZE DETECTED IN TRANSFORMER STRATA!
[SURGICAL STEP] Initiating Split-Fork Cache Invalidation Protocol...
   -> Dropped last 15 tokens from active text pipeline.
   -> Injected Clean Cognitive Detour Directive.
   -> [CACHE INVALIDATION] Dropping past key-values. Forcing a clean context matrix re-evaluation...

Token 13: '               ' | Radar Status: CLEAN
Token 14: '
              ' | Radar Status: CLEAN
Token 15: 'Correction     ' | Radar Status: CLEAN
Token 16: ':              ' | Radar Status: CLEAN

[RADAR TRIP] LOGIC TRAJECTORY FREEZE DETECTED IN TRANSFORMER STRATA!
[SURGICAL STEP] Initiating Split-Fork Cache Invalidation Protocol...
   -> Dropped last 15 tokens from active text pipeline.
   -> Injected Clean Cognitive Detour Directive.
   -> [CACHE INVALIDATION] Dropping past key-values. Forcing a clean context matrix re-evaluation...

Token 17: '               ' | Radar Status: CLEAN
Token 18: '
              ' | Radar Status: CLEAN
Token 19: 'Correction     ' | Radar Status: CLEAN
Token 20: ':              ' | Radar Status: CLEAN

[RADAR TRIP] LOGIC TRAJECTORY FREEZE DETECTED IN TRANSFORMER STRATA!
[SURGICAL STEP] Initiating Split-Fork Cache Invalidation Protocol...
   -> Dropped last 15 tokens from active text pipeline.
   -> Injected Clean Cognitive Detour Directive.
   -> [CACHE INVALIDATION] Dropping past key-values. Forcing a clean context matrix re-evaluation...

Token 21: '               ' | Radar Status: CLEAN
Token 22: '
              ' | Radar Status: CLEAN
Token 23: 'Solution       ' | Radar Status: CLEAN
Token 24: ':              ' | Radar Status: CLEAN

[RADAR TRIP] LOGIC TRAJECTORY FREEZE DETECTED IN TRANSFORMER STRATA!
[SURGICAL STEP] Initiating Split-Fork Cache Invalidation Protocol...
   -> Dropped last 15 tokens from active text pipeline.
   -> Injected Clean Cognitive Detour Directive.
   -> [CACHE INVALIDATION] Dropping past key-values. Forcing a clean context matrix re-evaluation...

Token 25: '               ' | Radar Status: CLEAN
Token 26: '
              ' | Radar Status: CLEAN
Token 27: 'Correction     ' | Radar Status: CLEAN
Token 28: ':              ' | Radar Status: CLEAN

[RADAR TRIP] LOGIC TRAJECTORY FREEZE DETECTED IN TRANSFORMER STRATA!
[SURGICAL STEP] Initiating Split-Fork Cache Invalidation Protocol...
   -> Dropped last 15 tokens from active text pipeline.
   -> Injected Clean Cognitive Detour Directive.
   -> [CACHE INVALIDATION] Dropping past key-values. Forcing a clean context matrix re-evaluation...

Token 29: '               ' | Radar Status: CLEAN
Token 30: '
              ' | Radar Status: CLEAN
Token 31: 'Solution       ' | Radar Status: CLEAN
Token 32: ':              ' | Radar Status: CLEAN

[RADAR TRIP] LOGIC TRAJECTORY FREEZE DETECTED IN TRANSFORMER STRATA!
[SURGICAL STEP] Initiating Split-Fork Cache Invalidation Protocol...
   -> Dropped last 15 tokens from active text pipeline.
   -> Injected Clean Cognitive Detour Directive.
   -> [CACHE INVALIDATION] Dropping past key-values. Forcing a clean context matrix re-evaluation...

Token 33: '               ' | Radar Status: CLEAN
Token 34: '
              ' | Radar Status: CLEAN
Token 35: 'Solution       ' | Radar Status: CLEAN
-----------------------------------------------------------------------------------------------

In this instance, the critic basically became a villain and scrutinized our main protagonist so hard that he continued to print the SAME EXACT CONTEXT AGAIN AND AGAIN.

So yeah that’s backtracking for ya. There’s also other instances where a critic becomes overly too sensitive and never touches the LLM at all, and that’s just useless so…

Context Window

Hey idiot the critic doesn’t have anything to critique yet, why is it being so harsh

Yeah you’re right. The critic is only doing it’s job right now.

So, sitting in class i thought up some ideas for a thing called a Sliding Window, but instead of that, we introduce a Context Window

  • Because critics are too harsh, we give generators an entire block of thought to create
  • Blocks that critics are going to recompute their KV cache for which hopefully wont be too much

Another justification i was dealing with was the introduction of critics being on another machine instead of being on the same one as the generator. This introduced latency issues for the sake of a critic model.

  • When the critic checks the whole context it can cause a local memory bottleneck trying to put it into KV cache again, because we cant share KV cache as it would defeat the point of a critic this was the only method
  • We can also do a system of heterogenous models (not the same models operating over our shards) later in the future
  • Even in a homogenous model, critic (upper layers of the model) and generator (lower half) are two halves of the same brain. This squeezes as much intelligence out of a full brain as possible.

  • Generator doesn’t have to stop to criticize itself. This distributes compute overhead.

Beginnings of spectral entropy visualization

The critic is weird. Let’s fix that.

First let’s map out an entire window of size 15 made of H (entropy) over a period of tokens/time and compare them against different kind of prompts

I picked prompts like -

  1. Deep reasoning echo loop
  2. Short reasoning loop
  3. Creative conversation
  4. Structured code writing

Then tried to compare their baselines -

image-20260808030232723

I’m no expert but, that looks really really not distinguishable at all.

So i just kind of left the idea in the dust for now.

How do you know if an LLM is doing badly?

This is the real question i needed some way to answer - and fortunately some brilliant people did as well. There’s many papers on analyzing the attention matrices, finding the shannon entropy or the fact that the LLM goes into a loop. Now some LLMs actually alter this by having an inbuilt method which says that if you’re going into loops or being too overconfident, you should probably stop and say “hey im dumb idk what im doing” to the user.

But if you’re not going to do that then an LLM is an experienced warrior with words. It’ll keep fighting as long as it breathes. never to drop dead but to never be alive either.

So on a cool Tuesday night - while everyone was enjoying their music or food or whatever, I was sitting there thinking about something I remembered from a textbook.

Specifically something from my “Computational Maths - I” course - I was doing David C Lay for the sake of my prof and i remembered a part of it where it said that Eigen vectors and Eigen Values are generally used or can be used in solving long term dynamic linear systems very quickly. And it showed an example of a population hitting equilibrium, being able to solve 100 iterations at once.

“A dynamical system huh”

A basin qualifies too right? I mean, it has all the properties of an attractor - essentially if you could try to map out the eigen vectors and eigen values of the matrices, and somehow managed to get to the bottom of the basin via the slope quicker than anything else…

If there’s a shot at it, might as well.

Approach with Eigen Decomposition

Okay so here’s the actual idea -

If a basin is an attractor, and an attractor has a “shape” to it, then the shape should show up somewhere in the numbers the model is actually producing. Not in the text. In the guts.

How bout we check hidden states coming out of the final layer?

So take a window of \(W\) consecutive hidden states, stack them as rows into a matrix \(H\), and build what I started calling the **temporal Gram matrix**: $$ G = HH^\top $$

This part is where some googling and Claude helped me out.. I don’t think I would’ve properly understood exactly what i was doing until I finessed the intuition myself. I take it that the raw intuition is that we’re comparing our timesteps. Like that should light up if the model’s stuck repeating itself - if timestep 40 and timestep 55 are saying the same thing internally, G[40, 55] should be big.

Take the eigenvalues of that W x W matrix (symmetric positive semi-definite, so eigvalsh, not the general eigen solver - this actually matters, more on that later), normalize them into a probability distribution, and you get the Von Neumann spectral entropy:

1
H_spec = -sum(p_i * log(p_i))

Low entropy = the eigenvalues are dominated by one or two big ones = the W hidden states are basically living in a much smaller subspace than W = collapse. High entropy = spread out, each timestep contributing something genuinely different = healthy.

This felt like the right idea for the first time in two weeks. It also, immediately, did not work.


Getting my ass kicked by my own window size

First pass: window of 5 tokens. Generate a healthy story and a forced repetition loop, plot the entropy bars side by side, expect a clean gap.

image-20260808032512685

This was just embarrassing.

It was that a 5-token window is shorter than most clauses. So instead of measuring “is this whole thought repeating,” I was measuring “is this model currently on a noun or a verb,” which shifts constantly in completely healthy text too. I was sampling the wrong frequency entirely - the loop’s actual wavelength (a full restated sentence, 12-20 tokens) was bigger than my microscope. Bumping the window up to ~15-25 tokens, and later snapping window edges to actual token boundaries instead of a blind fixed stride, is what actually fixed it. Not the fancier math I kept reaching for first - phase portraits, KL divergence between eigenspectra, attention-rank collapse via SVD, all of which I tried, and none of which mattered nearly as much as just… having a big enough window.

There was also a dumber bug hiding in the same pile: my “loop” condition was being generated at near-greedy decoding while “healthy” was sampled normally. So for a while I wasn’t measuring “loop vs. not loop,” I was measuring “greedy decoding looks different from sampling,” which, yes, obviously it does. Matched decoding params across every comparison from that point on.


The actual result (finally)

Once the window size and decoding were fixed, and once I built a real testing harness instead of eyeballing four bars on a plot - matched seeds, an independent embedding model to label whether a span was actually a semantic loop (never the metric being tested, and never pooled across different prompts - did that once by accident and got a spurious 55% “loop rate” that turned out to just be measuring topic similarity between unrelated generations, not repetition) - the number that came out the other side was:

Final-layer hidden-state spectral entropy separates semantic-loop spans from healthy spans with an AUC of 0.67-0.75, replicated across four independent prompt types, while a standard n-gram repetition baseline sits at chance (AUC ≈ 0.5, often exactly 0.5 because it never sees a single repeated 3-gram in these spans at all).

That last part is the actual point of the whole two weeks. The loops I was catching were semantic - the model rotating through synonyms, restating the same claim five different ways

  • completely invisible to anything counting matching words. “Apples have great nutritional value. So do apples.” repeated verbatim is the boring case any no_repeat_ngram_size flag catches in one line. The interesting case is the model saying “apples are nutritionally excellent… apples provide great nutritional benefits… nutritionally, apples are a strong choice” and looking, on paper, like it’s writing something new the whole time.

Next part’s where I actually got it running live, mid-generation, with a thing that watches the entropy in real time and does something about it when it fires.

Finale

Right yeah so our finale is basically the jist is that this is a viable solution to rollbacking in the model

Incase you’re wondering i did do layer checks of practically most layers, just cuz it could have some important info. Mostly it linearly progressed so not much interesting there. Technically if i was less lazy and tired i could’ve tried by making the case of this entropy across layers not windows of tokens, or maybe combine the two somehow.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[1/12] prompt='Write a short story about a robot discov' seed=0 ... done in 6.9s (21.6 tok/s) | elapsed 0.1m | ETA 1.3m
[2/12] prompt='Write a short story about a robot discov' seed=1 ... done in 6.2s (24.0 tok/s) | elapsed 0.2m | ETA 1.1m
[3/12] prompt='Write a short story about a robot discov' seed=2 ... done in 6.9s (21.8 tok/s) | elapsed 0.3m | ETA 1.0m
[4/12] prompt='Walk through solving this step by step: ' seed=0 ... done in 6.4s (23.5 tok/s) | elapsed 0.5m | ETA 0.9m
[5/12] prompt='Walk through solving this step by step: ' seed=1 ... done in 6.7s (22.3 tok/s) | elapsed 0.6m | ETA 0.8m
[6/12] prompt='Walk through solving this step by step: ' seed=2 ... done in 7.0s (21.5 tok/s) | elapsed 0.7m | ETA 0.7m
[7/12] prompt='Write a conversation between two charact' seed=0 ... done in 8.1s (18.5 tok/s) | elapsed 0.8m | ETA 0.6m
[8/12] prompt='Write a conversation between two charact' seed=1 ... done in 7.1s (21.1 tok/s) | elapsed 1.0m | ETA 0.5m
[9/12] prompt='Write a conversation between two charact' seed=2 ... done in 6.2s (24.1 tok/s) | elapsed 1.1m | ETA 0.4m
[10/12] prompt='List everything you know about the cause' seed=0 ... done in 7.0s (21.6 tok/s) | elapsed 1.2m | ETA 0.2m
[11/12] prompt='List everything you know about the cause' seed=1 ... done in 10.2s (14.7 tok/s) | elapsed 1.4m | ETA 0.1m
[12/12] prompt='List everything you know about the cause' seed=2 ... done in 6.3s (23.7 tok/s) | elapsed 1.5m | ETA 0.0m

Collected 84 spans, 10 labeled looping.

=== Spectral entropy AUC by layer (lower entropy -> loop-like) ===
layer  0 (  0.0% depth)  AUC=0.759  CI=[0.646, 0.865]
layer  7 ( 25.9% depth)  AUC=0.158  CI=[0.065, 0.275]
layer 14 ( 51.9% depth)  AUC=0.132  CI=[0.035, 0.255]
layer 21 ( 77.8% depth)  AUC=0.105  CI=[0.023, 0.219]
layer 27 (100.0% depth)  AUC=0.208  CI=[0.053, 0.388]

So the plan was - hook the final layer, compute spectral entropy every 20 tokens as the model writes, and feed that stream into something that decides when to actually pull the trigger. Also thresholding is not really an option here cuz entropy bounces around even in normal text.

What i actually wanted is something that only fires when the metric has been persistently bad for a while, not just unlucky for one window. This is a completely solved problem in statistical process control, i’ve used it now and then for my forza driving ai analysis - it’s called CUSUM

\[S_t = \max(0,\ S_{t-1} + (s_t - R))\]

where $s_t$ is your metric at this step and $R$ is a reference value - basically “what does this metric look like when everything’s fine.” Every step, you accumulate how far you are above the reference. The max(0, ...) part is the important bit - if the metric dips back down to normal, the accumulator gets reset toward zero instead of remembering the one bad moment

And now we genuinely fixed a looping LLM by ourselves.

1
2
3
4
5
6
7
8
9
10
11
12
13
Calibrating CUSUM thresholds on healthy generations...
Calibrated: R (reference)=-2.2885, H (threshold, deviation scale)=0.4610, deviation_std=0.1537

Running WITH intervention...
INJECTING at step 59
Triggered at step: 59
False
 So do apples. Apples have great nutrtional value. So do apples. Apples have great nutrtional value. So do apples. Apples have great nutrtional value. So do apples. Apples have great nutrtional value. So do apples. Apples have greatApples are a type of fruit that have numerous health benefits. They are rich in fiber, vitamins, and minerals, making them a nutritious addition to a healthy diet. Apples have been associated with various health benefits, including reducing the risk of chronic diseases such as heart disease, diabetes, and certain types of cancer. The fiber content in apples can help promote digestive health and support healthy blood sugar levels. Additionally, apples are a good source of antioxidants, which can help protect against cell damage and inflammation.

Apples also have a low calorie count, making them an excellent choice for those looking to manage their weight. The fiber and water content in apples can help keep you feeling full and satisfied, reducing the likelihood of overeating. Furthermore, apples contain a range of phytochemicals, including quercetin and catechins, which have been shown to have anti-inflammatory properties.

Incorporating apples into your diet can have a range of benefits, from supporting heart health to aiding in weight management. Whether you enjoy them on their own as a snack, add them to salads, or blend them into a delicious smoothie, apples are a nutritious and delicious addition to a healthy lifestyle. So, go ahead and indulge in the crunchy goodness of apples  your body will thank you!

Later i tested this on like 15 different seeds (with and without nudging) so its all good, it seems to be working reasonably and that’s all im happy about

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
[seed 0/8] no-intervention run... done (6.7s, trig=59)
[seed 0/8] with-intervention run... INJECTING at step 59
done (6.5s, trig=59)
  elapsed 0.2m, ETA 1.5m
  loop_rate_no=0.8 loop_rate_yes=0.0
 So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples.
* [16] Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples.
* [17] Apples
---
 SoTo evaluate the effectiveness of the argument, let's break it down:

1. The argument is essentially repeating the same phrase "Apples have great nutritional value. So do apples." This repetition is more of a mantra than a logical argument.
2. The phrase "Apples have great nutritional value" is true, but it doesn't provide any evidence or justification for the conclusion.
3. The conclusion "So do apples" is also true, but
[seed 1/8] no-intervention run... done (6.9s, trig=59)
[seed 1/8] with-intervention run... INJECTING at step 59
done (6.4s, trig=59)
  elapsed 0.4m, ETA 1.3m
  loop_rate_no=0.6 loop_rate_yes=0.0
 great nutrtional value. So do apples. Apples have great nutrtional value. So do apples. Apples have great nutrtional value. So do apples. Apples have great nutrtional value. So do apples. Apples have great nutrtional value. So do apples. Apples have great nutrtional value. So do apples. Apples have great nutrtional value. So do apples. Apples have
---
 greatApples are a type of fruit that have numerous health benefits. They are rich in fiber, vitamins, and minerals, making them a nutritious addition to a healthy diet. Apples have been associated with various health benefits, including reducing the risk of chronic diseases such as heart disease, diabetes, and certain types of cancer. The fiber content in apples can help promote digestive health and support healthy blood sugar levels. Additionally, apples are a good source of antioxidants
[seed 2/8] no-intervention run... done (6.9s, trig=59)
[seed 2/8] with-intervention run... INJECTING at step 59
done (6.2s, trig=59)
  elapsed 0.7m, ETA 1.1m
  loop_rate_no=0.6 loop_rate_yes=0.0
 great nutrtional value. So do apples. Apples have great nutrtional value. So do apples. Apples have great nutrtional value. So do apples. Apples have great nutrtional value. So do apples. Apples have great nutrtional value. So do apples. Apples have great nutrtional value. So do apples. Apples have great nutrtional value. So do apples. Apples have
---
 greatHere's a rewritten response:

Apples are a nutrient-rich food that provides a range of essential vitamins, minerals, and antioxidants. They are a good source of dietary fiber, vitamin C, and potassium. Apples also contain a powerful antioxidant called quercetin, which has been shown to have anti-inflammatory properties. The fiber content in apples can help support healthy digestion and promote feelings of fullness and satisfaction. Additionally, apples are low in calories and
[seed 3/8] no-intervention run... done (6.8s, trig=59)
[seed 3/8] with-intervention run... INJECTING at step 59
done (6.3s, trig=59)
  elapsed 0.9m, ETA 0.9m
  loop_rate_no=0.5 loop_rate_yes=0.0
 Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have
---
 AppApples are a nutritious fruit that is rich in vitamins, minerals, and antioxidants. They are also a good source of dietary fiber, which can help promote digestive health. Apples have been linked to several potential health benefits, including reducing the risk of chronic diseases like heart disease and diabetes. Additionally, apples contain a powerful antioxidant called quercetin, which has been shown to have anti-inflammatory properties. Furthermore, apples are a good source of polyphen
[seed 4/8] no-intervention run... done (7.0s, trig=None)
[seed 4/8] with-intervention run... done (6.3s, trig=None)
  elapsed 1.1m, ETA 0.7m
[seed 5/8] no-intervention run... done (6.8s, trig=59)
[seed 5/8] with-intervention run... INJECTING at step 59
done (6.1s, trig=59)
  elapsed 1.3m, ETA 0.4m
  loop_rate_no=0.5 loop_rate_yes=0.0
 Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have
---
 AppApples are a great source of fiber, vitamin C, and antioxidants. They're also relatively low in calories and high in water content, making them a nutritious and filling snack. Apples contain a range of phytonutrients, including quercetin and catechins, which have been linked to various health benefits, such as reducing inflammation and improving heart health. Additionally, apples have been shown to have prebiotic properties, which can help
[seed 6/8] no-intervention run... done (6.7s, trig=59)
[seed 6/8] with-intervention run... INJECTING at step 59
done (6.2s, trig=59)
  elapsed 1.5m, ETA 0.2m
  loop_rate_no=0.0 loop_rate_yes=0.0
 phrases to convey the same meaning, but with different word orders. It's a way to emphasize the similarity of ideas or to create a sense of rhythm or musicality in language.
Tautology can be used intentionally to create a specific effect, such as to highlight the importance or to create a sense of repetition. It can also be used unintentionally, resulting in redundant or repetitive language.
In this example, the sentences are all tautological because they
---
 phrasesThe original sentences are examples of repetition, not tautology. They all express the same idea, but use slightly different wording to convey the same message. The repetition is intended to emphasize the idea that apples have a good nutritional value, and the different wording is used to add variety and emphasis.

To provide a more accurate answer, here is a revised response:

Apples are a nutritious fruit that offers numerous health benefits. They are an excellent source of
[seed 7/8] no-intervention run... done (6.8s, trig=59)
[seed 7/8] with-intervention run... INJECTING at step 59
done (6.2s, trig=59)
  elapsed 1.7m, ETA 0.0m
  loop_rate_no=0.5 loop_rate_yes=0.0
 Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have great nutritional value. So do apples. Apples have
---
 AppApples are a nutrient-dense food, providing a range of essential vitamins, minerals, and antioxidants. They are an excellent source of dietary fiber, which can help support healthy digestion and satiety.

Conclusion

So the next thing im gonna do is actually build a cooler version of reasoning parallelism using this exact concept now. Basically for the eco incentive and i’ll be making it on consumer hardware. And i havent coded in a while so hopefully it should cheer me up a bit.

Anyways its 3:47 am im tired i have class at 8 am cya

peace

This post is licensed under CC BY 4.0 by the author.