Stealing a Trick from Nuclear Physics to Solve a Puzzle
Jane Street dropped a neural net puzzle. This neural network-based puzzle was also mentioned on a Dwarkesh podcast, where he does an excellent job on explaining this puzzle. It's worth a quick watch.
While this is a bit before my time but I am assuming that the title to this puzzle is a reference to the terror of dropping punch cards on the floor and being unable to put them back together in the right feed order so that you can run your program.
Oops! I am not sure how you'd put the punch card in the correct order if they were not labeled but for a neural network, we might have some hints in the weights themselves. Hmm....
For some helpful context, in case you didn't check out the puzzle details, a trained 48-block (97 layer) ResNet fell apart into 97 shuffled layers. It's our job to the put the 97 layers together but we do get the data it was trained on.
Where we get a break is that its implied that the correct order would result in a loss of 0 across all of its recorded predictions (the pred column in the dataset).
So Shawn Park already solved it, and spoiler alert: my solution builds on it and makes it one order of magnitude faster.
Keep reading to see how :-)!
Overall Strategy
The original solver effectively has three steps:
-
Pair. Each block was split into two NN layers. A simple check, and remembering our linear algebra matrix multiply rules, for each layer shape reduces the search space quite a bit from 97! to (48!)² possible combinations.
Then, a fingerprint in the weights themselves matches each layer to its partner: for true pairs, multiplying the two weight matrices together gives a strongly negative diagonal. That takes us from (48!)² down to 48!.
-
Guess a likely order. Sort the blocks by the size of their relative weights (originally L2).
The hunch here is that later layers tend to have bigger weights vs earlier ones. This further reduces things from 48! to aprox. 10²⁷ possible combinations.
-
Swap local pairs until the loss is 0 Try swapping neighboring compatible layers, run the forward pass and only keep a swap if the error improves, repeat until nothing helps or we reach 0 loss.
This finally narrows things down to the one right order, after trying about 10^2.7 possible combinations (~550 or so).
Note that for each combination we have to run at least one full forward pass through the network to check the error. As it turns out, this part is pretty expensive, since we have to do 96 sequential matrix multiply's, or about 0.00088 GFLOPs per sample. To be sure, we run 1000 samples for each check, so that's 0.88 GFLOP per swap.
On my machine, steps 1 and 2 take well under a second (this includes script setup time). Step 3 takes about 3-4 seconds (about 95% of the solve time). Perhaps step 3 is where we should focus on...
TL;DR
My hunch: from my vague memory, physicists or engineers (nuclear ones especially) must have this problem too. They deal in the world of atoms and not bits and can't afford to re-run a whole experiment every time they want to know what a small change might do. So we (w/ Claude ofc) went looking...
The intuition here is: Is it possible to cheaply predict what a potential layer swap's effect on the overall error instead of running the full forward pass again over the entire network? Even if we are able to predict this, it will only work if the prediction is computationally cheaper to perform vs actually re-running the forward pass for the current block combination.
Well it seems that there might be such a method and it's called the adjoint state method. And it is indeed cheaper to compute vs re-running inference on the full neural network.
For a sense of scale: a full forward pass over the neural network is 96 GEMMs (GEneral Matrix Multiply), since we have 97 layers to compute. On the other hand, one adjoint prediction is just 4 GEMMs plus a dot product.
The one catch is that for the adjoint computation, we do still perform one full forward and one backward pass, but that gets amortized across all of the predictions.
Also, its worth calling out that this strategy doesn't further reduce the search space (we still check a similar amount of combinations), it instead reduces the amount of work we have to do for each forward pass check in step 3.
In total, we now only have to perform 38 GFLOPs instead of 491 GFLOPs (counted with PyTorch's FLOP counter), or about an order of magnitude less ops than the original solution.
This has been a solved problem in nuclear engineering for quite some time
Reactor designers/engineers had a similar problem. For example, what happens if you tweak one material, or move one control rod? You can re-run your model or your experiment of course! But as it turns out in the world of atoms and not bits, re-running the full simulation for every possible tweak is way too expensive.
Back when this method was documented, they were doing this math by hand and if lucky on punch-card machines (perhaps this is what the title of this puzzle is alluding to?!).
The earliest I could find this strategy is from 1945 (review). Basically, solve the linear system once forward and another time backward. The backward solve gives you the importance of every point in the reactor (read: how much the final result cares about a change). You then multiply a potential small local change by its importance and you should know its effect on the whole reactor. Oak Ridge's SCALE tools still work on this basic concept.
Details on the math for this particular problem are here.
What is the practical speedup?
On my Intel-based box (an i9-13900F, CPU only, w/ SIMD acceleration), its about 20X faster overall (the L1 seed, early stopping and the adjoint trick combined):
| Solve time | Whole script | |
|---|---|---|
| Park's original (pre-L1) | 4.0 s | 4.97 s |
| Mine | 0.19 s | 1.28 s |
Fun part: the solve is now so fast that the solution startup time takes longer thant the solve itself. Ha!
Also of note, that as of writing this, this is also the fastest solution that I could find online. If this is no longer the case, let me know!
Code
The solver, benchmark and an academic-style write-up are on GitHub. Park's original solver is here, and his write-up here.