Publish hand-written binary maze solver and CUDA benchmark
Browse files- README.md +37 -0
- SHA256SUMS +6 -0
- bench.cu +74 -0
- checkpoint.json +349 -0
- prepare.py +26 -0
- results.json +24 -0
- written_maze.py +123 -0
README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
tags:
|
| 3 |
+
- maze-solving
|
| 4 |
+
- hard-attention
|
| 5 |
+
- weight-space-programming
|
| 6 |
+
- cuda
|
| 7 |
+
- binary
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# Hand-written binary maze solver
|
| 11 |
+
|
| 12 |
+
A train-free recurrent hard-attention network for exact shortest paths in unweighted, four-neighbour grids. Four fixed neighbour reads and a signed threshold block are reused at every cell and loop. A goal wave discovers cells at shortest-path distance; the first-discovery parent lets a second wave mark a shortest route. It is not a language model or a reproduction of the published softmax maze transformer.
|
| 13 |
+
|
| 14 |
+
- **48** nonzero signed connections (+1/-1), **18** integer thresholds per lane. The **96-connection** configuration applies two independent copies to different mazes. Both share one compiled CUDA kernel; doubling throughput does not increase single-maze capability.
|
| 15 |
+
- **Seven bits per cell**: visited, four one-hot parent directions, current path tip and on-path flag. CUDA packs them in one byte, plus the separate fixed input grid. No optimizer, training data or learned parameters.
|
| 16 |
+
- **61/61** deterministic generated/edge cases matched on each of the two GPUs in both configurations: 59 shortest paths, including 100-by-100 grids, and two blocked/unreachable cases. CPU logical maximum observed: 2,656 iterations. GPU termination checks are batched, so kernel launches need not equal logical steps.
|
| 17 |
+
|
| 18 |
+
| GPU | One lane | Two lanes |
|
| 19 |
+
| --- | ---: | ---: |
|
| 20 |
+
| GTX 1660 SUPER | 61.921 ms | 38.720 ms |
|
| 21 |
+
| RTX 2080 Ti | 74.037 ms | 47.691 ms |
|
| 22 |
+
|
| 23 |
+
Times are median of five warmed complete 61-case passes after a warmup, with device transfers and synchronization for each solve. Excludes fixture creation, compilation, CUDA startup and output comparison. Timings are for these exact existing hosts and a tiny workload, not a general device ranking.
|
| 24 |
+
|
| 25 |
+
## Run
|
| 26 |
+
|
| 27 |
+
```bash
|
| 28 |
+
python -m pip install numpy
|
| 29 |
+
python written_maze.py checkpoint.json
|
| 30 |
+
python prepare.py
|
| 31 |
+
nvcc -O3 -arch=sm_75 bench.cu -o bench
|
| 32 |
+
./bench
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
The first command after installing NumPy runs the reference from the checkpoint. `prepare.py` regenerates CPU-verified fixtures and the CUDA gate header from the same JSON; the CUDA benchmark checks every output mask against them. For a different NVIDIA architecture use a compatible compiler target. No externally sourced datasets or weights are included.
|
| 36 |
+
|
| 37 |
+
Inspired by [Looped Weight-Space Programming](https://mbzuai-oryx.github.io/LoopedWSP/); this is a separate implementation with hard-wired grid neighbourhoods and exact binary state.
|
SHA256SUMS
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
f99395a04d43054e080a25be0df7ed0691a93664a27dcf7fb82ff5f4de020443 README.md
|
| 2 |
+
5ed0257f2c107b984b40ed2a37eaf1f28934e1d7d393673d5b90188fd98c31f7 checkpoint.json
|
| 3 |
+
d16393ce07b4524538e9a1af45bee9b6a2c86400b236e12058fd6dd44f1e8b89 written_maze.py
|
| 4 |
+
f54d4833fcc86f34b1f1f2416de836679589581b5f332142ed140cc8a37924f5 prepare.py
|
| 5 |
+
d24822cb85a60421b4dfb019ca2072fc1ea6f13ac3f1e61ebf958a03f5ffbc85 bench.cu
|
| 6 |
+
6b4bebe3c5cb8dc67a0c93c685fc6970211aa7b4f12694498adf044c0ef9d5df results.json
|
bench.cu
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <cuda_runtime.h>
|
| 2 |
+
#include <algorithm>
|
| 3 |
+
#include <chrono>
|
| 4 |
+
#include <cstdint>
|
| 5 |
+
#include <cstdio>
|
| 6 |
+
#include <fstream>
|
| 7 |
+
#include <vector>
|
| 8 |
+
using B=uint8_t;
|
| 9 |
+
#define C(x) do{auto e=(x);if(e){fprintf(stderr,"%s at %d\n",cudaGetErrorString(e),__LINE__);exit(2);}}while(0)
|
| 10 |
+
struct Meta{int h,w,a,b,k,reachable;};
|
| 11 |
+
struct Case {Meta m;std::vector<B> g,expect;};
|
| 12 |
+
#define bit(x,n) (((x)>>(n))&1)
|
| 13 |
+
__global__ void step(const Meta* meta,const B* grid,const B* src,B* dst,int* alive,int stride) {
|
| 14 |
+
int i=blockIdx.x*blockDim.x+threadIdx.x, lane=blockIdx.y;
|
| 15 |
+
const Meta M=meta[lane]; if(i>=M.h*M.w)return;
|
| 16 |
+
int j=i+lane*stride,y=i/M.w,x=i%M.w;
|
| 17 |
+
int a0=y?src[j-M.w]:0, a1=x+1<M.w?src[j+1]:0,
|
| 18 |
+
a2=y+1<M.h?src[j+M.w]:0,a3=x?src[j-1]:0, z=src[j];
|
| 19 |
+
int open=grid[j],start=i==M.a;
|
| 20 |
+
int seen=bit(z,0),tip=bit(z,5),path=bit(z,6);
|
| 21 |
+
int p0=bit(z,1),p1=bit(z,2),p2=bit(z,3),p3=bit(z,4);
|
| 22 |
+
int s0=bit(a0,0),s1=bit(a1,0),s2=bit(a2,0),s3=bit(a3,0);
|
| 23 |
+
int t0=bit(a0,5),t1=bit(a1,5),t2=bit(a2,5),t3=bit(a3,5);
|
| 24 |
+
int pc0=bit(a0,3),pc1=bit(a1,4),pc2=bit(a2,1),pc3=bit(a3,2);
|
| 25 |
+
#include "gates.h"
|
| 26 |
+
dst[j]=next_seen|(next_parent0<<1)|(next_parent1<<2)|(next_parent2<<3)|(next_parent3<<4)|(next_tip<<5)|(next_path<<6);
|
| 27 |
+
if(next_tip || (next_seen!=seen))atomicOr(alive,1<<lane);
|
| 28 |
+
}
|
| 29 |
+
int main(){
|
| 30 |
+
cudaDeviceProp dp{};C(cudaGetDeviceProperties(&dp,0));C(cudaFree(nullptr)); puts(dp.name);
|
| 31 |
+
std::ifstream f("cases.bin",std::ios::binary);int count;f.read((char*)&count,4);if(!f||count!=61)return 3;
|
| 32 |
+
std::vector<Case> cases(count);int stride=0;
|
| 33 |
+
for(auto &z:cases){f.read((char*)&z.m,sizeof(Meta));int n=z.m.h*z.m.w;if(!f||n<1||n>11000)return 3;stride=std::max(stride,n);z.g.resize(n);z.expect.resize(n);f.read((char*)z.g.data(),n);f.read((char*)z.expect.data(),n);}
|
| 34 |
+
if(!f)return 3;
|
| 35 |
+
B *a,*b,*grid;Meta* meta;int* alive;size_t bytes=2*stride;
|
| 36 |
+
C(cudaMalloc(&a,bytes));C(cudaMalloc(&b,bytes));C(cudaMalloc(&grid,bytes));C(cudaMalloc(&meta,2*sizeof(Meta)));C(cudaMalloc(&alive,sizeof(int)));
|
| 37 |
+
for(int lanes: {1,2}){
|
| 38 |
+
std::vector<double> samples;int checks=0;int launched=0;
|
| 39 |
+
for(int trial=0;trial<6;trial++){
|
| 40 |
+
double sec=0;checks=0;launched=0;
|
| 41 |
+
for(int k=0;k<count;k+=lanes){
|
| 42 |
+
int m=std::min(lanes,count-k);
|
| 43 |
+
std::vector<B> init(m*stride,0),g(m*stride,0),out(m*stride,0);Meta d[2];
|
| 44 |
+
for(int l=0;l<m;l++){auto& z=cases[k+l];d[l]=z.m;int off=l*stride;std::copy(z.g.begin(),z.g.end(),g.begin()+off);
|
| 45 |
+
if(z.g[z.m.a]&&z.g[z.m.b])init[off+z.m.b]=1|(z.m.a==z.m.b?64:0);
|
| 46 |
+
else {std::fill(g.begin()+off,g.begin()+off+stride,0);}}
|
| 47 |
+
auto begin=std::chrono::steady_clock::now();
|
| 48 |
+
C(cudaMemcpy(grid,g.data(),m*stride,cudaMemcpyHostToDevice));
|
| 49 |
+
C(cudaMemcpy(a,init.data(),m*stride,cudaMemcpyHostToDevice));
|
| 50 |
+
C(cudaMemcpy(meta,d,m*sizeof(Meta),cudaMemcpyHostToDevice));
|
| 51 |
+
int rounds=0, act=1;
|
| 52 |
+
for(;act&&rounds<2*stride+16;){
|
| 53 |
+
for(int j=0;j<16;j++){
|
| 54 |
+
C(cudaMemsetAsync(alive,0,sizeof(int)));
|
| 55 |
+
step<<<dim3((stride+127)/128,m),128>>>(meta,grid,a,b,alive,stride);
|
| 56 |
+
std::swap(a,b);rounds++;
|
| 57 |
+
}
|
| 58 |
+
C(cudaGetLastError());
|
| 59 |
+
C(cudaMemcpy(&act,alive,sizeof(int),cudaMemcpyDeviceToHost));
|
| 60 |
+
}
|
| 61 |
+
if(act)return 5;
|
| 62 |
+
C(cudaMemcpy(out.data(),a,m*stride,cudaMemcpyDeviceToHost));
|
| 63 |
+
sec+=std::chrono::duration<double>(std::chrono::steady_clock::now()-begin).count();launched+=rounds;
|
| 64 |
+
for(int l=0;l<m;l++){
|
| 65 |
+
auto& z=cases[k+l]; for(size_t j=0;j<z.expect.size();j++) if(bit(out[l*stride+j],6)!=z.expect[j]){fprintf(stderr,"path mismatch case %d cell %zu\n",k+l,j);return 4;}
|
| 66 |
+
checks++;
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
if(trial) samples.push_back(sec);
|
| 70 |
+
}
|
| 71 |
+
std::sort(samples.begin(),samples.end());
|
| 72 |
+
printf("LANES=%d CHECKS=%d MEDIAN_SEC=%.6f MIN=%.6f MAX=%.6f LAUNCH_ROUNDS=%d STATE_BYTES_PER_CELL=1 LIVE_DEVICE_BYTES=%zu\n",lanes,checks,samples[2],samples[0],samples[4],launched,3*bytes+2*sizeof(Meta)+sizeof(int));
|
| 73 |
+
}
|
| 74 |
+
}
|
checkpoint.json
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"format": "sg-maze-signed-threshold-v1",
|
| 3 |
+
"offsets": [
|
| 4 |
+
[
|
| 5 |
+
-1,
|
| 6 |
+
0
|
| 7 |
+
],
|
| 8 |
+
[
|
| 9 |
+
0,
|
| 10 |
+
1
|
| 11 |
+
],
|
| 12 |
+
[
|
| 13 |
+
1,
|
| 14 |
+
0
|
| 15 |
+
],
|
| 16 |
+
[
|
| 17 |
+
0,
|
| 18 |
+
-1
|
| 19 |
+
]
|
| 20 |
+
],
|
| 21 |
+
"state_bits": [
|
| 22 |
+
"seen",
|
| 23 |
+
"parent_north",
|
| 24 |
+
"parent_east",
|
| 25 |
+
"parent_south",
|
| 26 |
+
"parent_west",
|
| 27 |
+
"tip",
|
| 28 |
+
"on_path"
|
| 29 |
+
],
|
| 30 |
+
"inputs": [
|
| 31 |
+
"open",
|
| 32 |
+
"start",
|
| 33 |
+
"goal"
|
| 34 |
+
],
|
| 35 |
+
"lane_configs": [
|
| 36 |
+
{
|
| 37 |
+
"lanes": 1,
|
| 38 |
+
"signed_weights": 48,
|
| 39 |
+
"thresholds": 18
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"lanes": 2,
|
| 43 |
+
"signed_weights": 96,
|
| 44 |
+
"thresholds": 36
|
| 45 |
+
}
|
| 46 |
+
],
|
| 47 |
+
"gates": [
|
| 48 |
+
{
|
| 49 |
+
"name": "near",
|
| 50 |
+
"threshold": 1,
|
| 51 |
+
"edges": [
|
| 52 |
+
[
|
| 53 |
+
"s0",
|
| 54 |
+
1
|
| 55 |
+
],
|
| 56 |
+
[
|
| 57 |
+
"s1",
|
| 58 |
+
1
|
| 59 |
+
],
|
| 60 |
+
[
|
| 61 |
+
"s2",
|
| 62 |
+
1
|
| 63 |
+
],
|
| 64 |
+
[
|
| 65 |
+
"s3",
|
| 66 |
+
1
|
| 67 |
+
]
|
| 68 |
+
]
|
| 69 |
+
},
|
| 70 |
+
{
|
| 71 |
+
"name": "new",
|
| 72 |
+
"threshold": 2,
|
| 73 |
+
"edges": [
|
| 74 |
+
[
|
| 75 |
+
"open",
|
| 76 |
+
1
|
| 77 |
+
],
|
| 78 |
+
[
|
| 79 |
+
"near",
|
| 80 |
+
1
|
| 81 |
+
],
|
| 82 |
+
[
|
| 83 |
+
"seen",
|
| 84 |
+
-1
|
| 85 |
+
]
|
| 86 |
+
]
|
| 87 |
+
},
|
| 88 |
+
{
|
| 89 |
+
"name": "pick0",
|
| 90 |
+
"threshold": 2,
|
| 91 |
+
"edges": [
|
| 92 |
+
[
|
| 93 |
+
"new",
|
| 94 |
+
1
|
| 95 |
+
],
|
| 96 |
+
[
|
| 97 |
+
"s0",
|
| 98 |
+
1
|
| 99 |
+
]
|
| 100 |
+
]
|
| 101 |
+
},
|
| 102 |
+
{
|
| 103 |
+
"name": "arrive0",
|
| 104 |
+
"threshold": 2,
|
| 105 |
+
"edges": [
|
| 106 |
+
[
|
| 107 |
+
"t0",
|
| 108 |
+
1
|
| 109 |
+
],
|
| 110 |
+
[
|
| 111 |
+
"pc0",
|
| 112 |
+
1
|
| 113 |
+
]
|
| 114 |
+
]
|
| 115 |
+
},
|
| 116 |
+
{
|
| 117 |
+
"name": "pick1",
|
| 118 |
+
"threshold": 2,
|
| 119 |
+
"edges": [
|
| 120 |
+
[
|
| 121 |
+
"new",
|
| 122 |
+
1
|
| 123 |
+
],
|
| 124 |
+
[
|
| 125 |
+
"s1",
|
| 126 |
+
1
|
| 127 |
+
],
|
| 128 |
+
[
|
| 129 |
+
"s0",
|
| 130 |
+
-1
|
| 131 |
+
]
|
| 132 |
+
]
|
| 133 |
+
},
|
| 134 |
+
{
|
| 135 |
+
"name": "arrive1",
|
| 136 |
+
"threshold": 2,
|
| 137 |
+
"edges": [
|
| 138 |
+
[
|
| 139 |
+
"t1",
|
| 140 |
+
1
|
| 141 |
+
],
|
| 142 |
+
[
|
| 143 |
+
"pc1",
|
| 144 |
+
1
|
| 145 |
+
]
|
| 146 |
+
]
|
| 147 |
+
},
|
| 148 |
+
{
|
| 149 |
+
"name": "pick2",
|
| 150 |
+
"threshold": 2,
|
| 151 |
+
"edges": [
|
| 152 |
+
[
|
| 153 |
+
"new",
|
| 154 |
+
1
|
| 155 |
+
],
|
| 156 |
+
[
|
| 157 |
+
"s2",
|
| 158 |
+
1
|
| 159 |
+
],
|
| 160 |
+
[
|
| 161 |
+
"s0",
|
| 162 |
+
-1
|
| 163 |
+
],
|
| 164 |
+
[
|
| 165 |
+
"s1",
|
| 166 |
+
-1
|
| 167 |
+
]
|
| 168 |
+
]
|
| 169 |
+
},
|
| 170 |
+
{
|
| 171 |
+
"name": "arrive2",
|
| 172 |
+
"threshold": 2,
|
| 173 |
+
"edges": [
|
| 174 |
+
[
|
| 175 |
+
"t2",
|
| 176 |
+
1
|
| 177 |
+
],
|
| 178 |
+
[
|
| 179 |
+
"pc2",
|
| 180 |
+
1
|
| 181 |
+
]
|
| 182 |
+
]
|
| 183 |
+
},
|
| 184 |
+
{
|
| 185 |
+
"name": "pick3",
|
| 186 |
+
"threshold": 2,
|
| 187 |
+
"edges": [
|
| 188 |
+
[
|
| 189 |
+
"new",
|
| 190 |
+
1
|
| 191 |
+
],
|
| 192 |
+
[
|
| 193 |
+
"s3",
|
| 194 |
+
1
|
| 195 |
+
],
|
| 196 |
+
[
|
| 197 |
+
"s0",
|
| 198 |
+
-1
|
| 199 |
+
],
|
| 200 |
+
[
|
| 201 |
+
"s1",
|
| 202 |
+
-1
|
| 203 |
+
],
|
| 204 |
+
[
|
| 205 |
+
"s2",
|
| 206 |
+
-1
|
| 207 |
+
]
|
| 208 |
+
]
|
| 209 |
+
},
|
| 210 |
+
{
|
| 211 |
+
"name": "arrive3",
|
| 212 |
+
"threshold": 2,
|
| 213 |
+
"edges": [
|
| 214 |
+
[
|
| 215 |
+
"t3",
|
| 216 |
+
1
|
| 217 |
+
],
|
| 218 |
+
[
|
| 219 |
+
"pc3",
|
| 220 |
+
1
|
| 221 |
+
]
|
| 222 |
+
]
|
| 223 |
+
},
|
| 224 |
+
{
|
| 225 |
+
"name": "born",
|
| 226 |
+
"threshold": 2,
|
| 227 |
+
"edges": [
|
| 228 |
+
[
|
| 229 |
+
"start",
|
| 230 |
+
1
|
| 231 |
+
],
|
| 232 |
+
[
|
| 233 |
+
"new",
|
| 234 |
+
1
|
| 235 |
+
]
|
| 236 |
+
]
|
| 237 |
+
},
|
| 238 |
+
{
|
| 239 |
+
"name": "next_seen",
|
| 240 |
+
"threshold": 1,
|
| 241 |
+
"edges": [
|
| 242 |
+
[
|
| 243 |
+
"seen",
|
| 244 |
+
1
|
| 245 |
+
],
|
| 246 |
+
[
|
| 247 |
+
"new",
|
| 248 |
+
1
|
| 249 |
+
]
|
| 250 |
+
]
|
| 251 |
+
},
|
| 252 |
+
{
|
| 253 |
+
"name": "next_parent0",
|
| 254 |
+
"threshold": 1,
|
| 255 |
+
"edges": [
|
| 256 |
+
[
|
| 257 |
+
"p0",
|
| 258 |
+
1
|
| 259 |
+
],
|
| 260 |
+
[
|
| 261 |
+
"pick0",
|
| 262 |
+
1
|
| 263 |
+
]
|
| 264 |
+
]
|
| 265 |
+
},
|
| 266 |
+
{
|
| 267 |
+
"name": "next_parent1",
|
| 268 |
+
"threshold": 1,
|
| 269 |
+
"edges": [
|
| 270 |
+
[
|
| 271 |
+
"p1",
|
| 272 |
+
1
|
| 273 |
+
],
|
| 274 |
+
[
|
| 275 |
+
"pick1",
|
| 276 |
+
1
|
| 277 |
+
]
|
| 278 |
+
]
|
| 279 |
+
},
|
| 280 |
+
{
|
| 281 |
+
"name": "next_parent2",
|
| 282 |
+
"threshold": 1,
|
| 283 |
+
"edges": [
|
| 284 |
+
[
|
| 285 |
+
"p2",
|
| 286 |
+
1
|
| 287 |
+
],
|
| 288 |
+
[
|
| 289 |
+
"pick2",
|
| 290 |
+
1
|
| 291 |
+
]
|
| 292 |
+
]
|
| 293 |
+
},
|
| 294 |
+
{
|
| 295 |
+
"name": "next_parent3",
|
| 296 |
+
"threshold": 1,
|
| 297 |
+
"edges": [
|
| 298 |
+
[
|
| 299 |
+
"p3",
|
| 300 |
+
1
|
| 301 |
+
],
|
| 302 |
+
[
|
| 303 |
+
"pick3",
|
| 304 |
+
1
|
| 305 |
+
]
|
| 306 |
+
]
|
| 307 |
+
},
|
| 308 |
+
{
|
| 309 |
+
"name": "next_tip",
|
| 310 |
+
"threshold": 1,
|
| 311 |
+
"edges": [
|
| 312 |
+
[
|
| 313 |
+
"born",
|
| 314 |
+
1
|
| 315 |
+
],
|
| 316 |
+
[
|
| 317 |
+
"arrive0",
|
| 318 |
+
1
|
| 319 |
+
],
|
| 320 |
+
[
|
| 321 |
+
"arrive1",
|
| 322 |
+
1
|
| 323 |
+
],
|
| 324 |
+
[
|
| 325 |
+
"arrive2",
|
| 326 |
+
1
|
| 327 |
+
],
|
| 328 |
+
[
|
| 329 |
+
"arrive3",
|
| 330 |
+
1
|
| 331 |
+
]
|
| 332 |
+
]
|
| 333 |
+
},
|
| 334 |
+
{
|
| 335 |
+
"name": "next_path",
|
| 336 |
+
"threshold": 1,
|
| 337 |
+
"edges": [
|
| 338 |
+
[
|
| 339 |
+
"path",
|
| 340 |
+
1
|
| 341 |
+
],
|
| 342 |
+
[
|
| 343 |
+
"next_tip",
|
| 344 |
+
1
|
| 345 |
+
]
|
| 346 |
+
]
|
| 347 |
+
}
|
| 348 |
+
]
|
| 349 |
+
}
|
prepare.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from written_maze import *
|
| 2 |
+
import struct, re
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
load_checkpoint(Path(__file__).with_name('checkpoint.json'))
|
| 5 |
+
rng=np.random.default_rng(583812)
|
| 6 |
+
fixtures=[maze(15,rng) for _ in range(32)]+[maze(31,rng) for _ in range(16)]
|
| 7 |
+
for _ in range(8):
|
| 8 |
+
g=rng.random((100,100))>.30;g[0,:]=g[:,-1]=1;fixtures.append((g,(0,0),(99,99)))
|
| 9 |
+
fixtures.extend([(np.eye(13,dtype=bool),(0,0),(12,12)),(np.ones((4,6),dtype=bool),(1,2),(1,2)),(np.ones((31,31),dtype=bool),(0,0),(30,30)),(np.zeros((3,3),dtype=bool),(0,0),(2,2)),maze(101,rng)])
|
| 10 |
+
t0=t1=0.
|
| 11 |
+
with open('cases.bin','wb') as f:
|
| 12 |
+
f.write(struct.pack('<i',len(fixtures)))
|
| 13 |
+
for g,a,b in fixtures:
|
| 14 |
+
t=time.perf_counter();d=bfs(g,a,b);t0+=time.perf_counter()-t
|
| 15 |
+
t=time.perf_counter();mask,k=solve(g,a,b);t1+=time.perf_counter()-t
|
| 16 |
+
if d is None:
|
| 17 |
+
assert mask is None
|
| 18 |
+
mask=np.zeros_like(g)
|
| 19 |
+
else: assert bfs(mask,a,b)==d and int(mask.sum())==d+1 and (mask<=g).all() and k==2*d
|
| 20 |
+
h,w=g.shape
|
| 21 |
+
f.write(struct.pack('<6i',h,w,a[0]*w+a[1],b[0]*w+b[1],k,int(d is not None)))
|
| 22 |
+
f.write(g.astype(np.uint8).tobytes()); f.write(mask.astype(np.uint8).tobytes())
|
| 23 |
+
with open('gates.h','w',newline='\n') as f:
|
| 24 |
+
for out,(threshold,edges) in G.items():
|
| 25 |
+
f.write(re.sub(r'\bnew\b','new_seen','const int '+out+' = (0 '+''.join((' + ' if w>0 else ' - ')+k for k,w in edges)+') >= '+str(threshold)+';\n'))
|
| 26 |
+
print(f'CPU_BFS_SEC={t0:.6f} CPU_NETWORK_SEC={t1:.6f}')
|
results.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cases": 61,
|
| 3 |
+
"reachable": 59,
|
| 4 |
+
"blocked": 2,
|
| 5 |
+
"exact_mask_matches_per_arm": 61,
|
| 6 |
+
"median_warm_batch_ms": {
|
| 7 |
+
"GTX 1660 SUPER": [
|
| 8 |
+
61.921,
|
| 9 |
+
38.72
|
| 10 |
+
],
|
| 11 |
+
"RTX 2080 Ti": [
|
| 12 |
+
74.037,
|
| 13 |
+
47.691
|
| 14 |
+
]
|
| 15 |
+
},
|
| 16 |
+
"lanes": [
|
| 17 |
+
1,
|
| 18 |
+
2
|
| 19 |
+
],
|
| 20 |
+
"sample_passes": 5,
|
| 21 |
+
"fixture_sha256": "96f0fcc764e4750d911eba54fcc3fa80a1a4ac0070c0dcaf26c72f6612235664",
|
| 22 |
+
"cuda_gate_sha256": "b9187e24b7f873fee5311cbe75bb5834e158c559c7afb332c102590cdb3f34cc",
|
| 23 |
+
"cuda_source_sha256": "d24822cb85a60421b4dfb019ca2072fc1ea6f13ac3f1e61ebf958a03f5ffbc85"
|
| 24 |
+
}
|
written_maze.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""No learned parameters. Fixed local attention followed by layered signed threshold gates."""
|
| 2 |
+
import time
|
| 3 |
+
from collections import deque
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
OFF=(( -1,0),(0,1),(1,0),(0,-1))
|
| 7 |
+
G={}
|
| 8 |
+
def reg(out, threshold, pos, neg=()):
|
| 9 |
+
assert not (set(pos)&set(neg)) and len(set(pos+neg))==len(pos+neg)
|
| 10 |
+
G[out]=(threshold,[(k,1) for k in pos]+[(k,-1) for k in neg])
|
| 11 |
+
|
| 12 |
+
reg("near",1,tuple("s"+str(i) for i in range(4)))
|
| 13 |
+
reg("new",2,("open","near"),("seen",))
|
| 14 |
+
for i in range(4):
|
| 15 |
+
reg("pick"+str(i),2,("new","s"+str(i)),tuple("s"+str(j) for j in range(i)))
|
| 16 |
+
reg("arrive"+str(i),2,("t"+str(i),"pc"+str(i)))
|
| 17 |
+
reg("born",2,("start","new"))
|
| 18 |
+
reg("next_seen",1,("seen","new"))
|
| 19 |
+
for i in range(4):
|
| 20 |
+
reg("next_parent"+str(i),1,("p"+str(i),"pick"+str(i)))
|
| 21 |
+
reg("next_tip",1,("born",)+tuple("arrive"+str(i) for i in range(4)))
|
| 22 |
+
reg("next_path",1,("path","next_tip"))
|
| 23 |
+
COMPILED={o:(theta,[(k,w) for k,w in terms]) for o,(theta,terms) in G.items()}
|
| 24 |
+
|
| 25 |
+
def eval_gate(o, x):
|
| 26 |
+
t,terms=COMPILED[o]
|
| 27 |
+
a=x[terms[0][0]].astype(np.int8)*terms[0][1]
|
| 28 |
+
for k,w in terms[1:]:
|
| 29 |
+
if w==1: a+=x[k]
|
| 30 |
+
else: np.subtract(a,x[k],out=a,casting='unsafe')
|
| 31 |
+
return a>=t
|
| 32 |
+
|
| 33 |
+
def head(x,i): # Fixed one-hot attention; no wrap, zero when outside the grid.
|
| 34 |
+
dy,dx=OFF[i]
|
| 35 |
+
h,w=x.shape
|
| 36 |
+
z=np.zeros_like(x)
|
| 37 |
+
dest_y=slice(max(0,-dy),min(h,h-dy)); dest_x=slice(max(0,-dx),min(w,w-dx))
|
| 38 |
+
src_y=slice(max(0,dy),min(h,h+dy)); src_x=slice(max(0,dx),min(w,w+dx))
|
| 39 |
+
z[dest_y,dest_x]=x[src_y,src_x]
|
| 40 |
+
return z
|
| 41 |
+
|
| 42 |
+
def solve(grid,start,goal):
|
| 43 |
+
sh=grid.shape
|
| 44 |
+
if not grid[start] or not grid[goal]: return None,0
|
| 45 |
+
x={k:np.zeros(sh,dtype=bool) for k in ('start','seen','tip','path','p0','p1','p2','p3')}
|
| 46 |
+
x['open']=grid
|
| 47 |
+
x['start'][start]=True; x['seen'][goal]=True
|
| 48 |
+
if start==goal: x['path'][start]=True; return x['path'],0
|
| 49 |
+
for n in range(1,2*grid.size+1):
|
| 50 |
+
for i in range(4):
|
| 51 |
+
x[f's{i}']=head(x['seen'],i)
|
| 52 |
+
x[f't{i}']=head(x['tip'],i)
|
| 53 |
+
x[f'pc{i}']=head(x[f'p{(i+2)%4}'],i)
|
| 54 |
+
for o in G: x[o]=eval_gate(o,x)
|
| 55 |
+
if x['next_path'][goal]: return x['next_path'],n
|
| 56 |
+
if not x['new'].any() and not x['next_tip'].any(): return None,n
|
| 57 |
+
for k in ('seen','tip','path'): x[k]=x['next_'+k]
|
| 58 |
+
for i in range(4): x[f'p{i}']=x[f'next_parent{i}']
|
| 59 |
+
raise RuntimeError("bound exhausted")
|
| 60 |
+
|
| 61 |
+
def bfs(grid,start,goal):
|
| 62 |
+
sh=grid.shape
|
| 63 |
+
if not grid[start] or not grid[goal]: return None
|
| 64 |
+
q=deque([(start,0)]); seen={start}
|
| 65 |
+
while q:
|
| 66 |
+
(y,x),d=q.popleft()
|
| 67 |
+
if (y,x)==goal: return d
|
| 68 |
+
for a,b in OFF:
|
| 69 |
+
j,k=y+a,x+b
|
| 70 |
+
if 0<=j<sh[0] and 0<=k<sh[1] and grid[j,k] and (j,k) not in seen:
|
| 71 |
+
seen.add((j,k));q.append(((j,k),d+1))
|
| 72 |
+
return None
|
| 73 |
+
|
| 74 |
+
def maze(size,rng):
|
| 75 |
+
g=np.zeros((size,size),dtype=bool)
|
| 76 |
+
st=[(1,1)];g[1,1]=1
|
| 77 |
+
while st:
|
| 78 |
+
y,x=st[-1]; options=[]
|
| 79 |
+
for dy,dx in OFF:
|
| 80 |
+
a,b=y+2*dy,x+2*dx
|
| 81 |
+
if 1<=a<size-1 and 1<=b<size-1 and not g[a,b]: options.append((a,b))
|
| 82 |
+
if not options: st.pop();continue
|
| 83 |
+
a,b=options[int(rng.integers(len(options)))]
|
| 84 |
+
g[a,b]=1;g[(a+y)//2,(b+x)//2]=1;st.append((a,b))
|
| 85 |
+
return g,(1,1),(size-2,size-2)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def load_checkpoint(path):
|
| 89 |
+
import json
|
| 90 |
+
from pathlib import Path
|
| 91 |
+
doc=json.loads(Path(path).read_text(encoding='utf-8'))
|
| 92 |
+
assert doc['format']=='sg-maze-signed-threshold-v1'
|
| 93 |
+
assert doc['offsets']==[list(x) for x in OFF]
|
| 94 |
+
rows={}
|
| 95 |
+
for row in doc['gates']:
|
| 96 |
+
assert row['name'] not in rows
|
| 97 |
+
rows[row['name']]=(row['threshold'],[(a,b) for a,b in row['edges']])
|
| 98 |
+
assert list(rows)==list(G) and all(type(t) is int and all(type(w) is int and w in (-1,1) for _,w in es) for t,es in rows.values())
|
| 99 |
+
G.clear()
|
| 100 |
+
G.update(rows)
|
| 101 |
+
COMPILED.clear()
|
| 102 |
+
COMPILED.update(rows)
|
| 103 |
+
|
| 104 |
+
if __name__=="__main__":
|
| 105 |
+
import sys
|
| 106 |
+
if len(sys.argv)>1: load_checkpoint(sys.argv[1])
|
| 107 |
+
rng=np.random.default_rng(583812)
|
| 108 |
+
fixtures=[maze(15,rng) for _ in range(32)]+[maze(31,rng) for _ in range(16)]
|
| 109 |
+
for _ in range(8):
|
| 110 |
+
g=rng.random((100,100))>.30;g[0,:]=g[:,-1]=1
|
| 111 |
+
fixtures.append((g,(0,0),(99,99)))
|
| 112 |
+
fixtures.extend([(np.eye(13,dtype=bool),(0,0),(12,12)),(np.ones((4,6),dtype=bool),(1,2),(1,2)),(np.ones((31,31),dtype=bool),(0,0),(30,30)),(np.zeros((3,3),dtype=bool),(0,0),(2,2)),maze(101,rng)])
|
| 113 |
+
times=[0.,0.]; counts=[0,0]; max_loops=0
|
| 114 |
+
for g,a,b in fixtures:
|
| 115 |
+
t=time.perf_counter(); d=bfs(g,a,b);times[0]+=time.perf_counter()-t
|
| 116 |
+
t=time.perf_counter(); p,k=solve(g,a,b);times[1]+=time.perf_counter()-t
|
| 117 |
+
max_loops=max(k,max_loops)
|
| 118 |
+
if d is None: assert p is None;counts[0]+=1
|
| 119 |
+
else:
|
| 120 |
+
assert p is not None and p[a] and p[b] and (p<=g).all() and p.sum()==d+1 and bfs(p,a,b)==d
|
| 121 |
+
assert k==2*d
|
| 122 |
+
counts[1]+=1
|
| 123 |
+
print(dict(fixtures=len(fixtures),reachable=counts[1],unreachable_or_blocked=counts[0],signed_weights=sum(len(t) for _,t in G.values()),thresholds=len(G),max_loops=max_loops,bfs_sec=times[0],block_sec=times[1]))
|