File size: 4,117 Bytes
aff7262
 
349a2a7
 
aff7262
 
 
 
349a2a7
0d1e2af
 
aff7262
 
349a2a7
 
aff7262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
349a2a7
 
 
 
 
 
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
---
license: other
license_name: snapkitty-tri-license
license_link: https://huggingface.co/Snapkitty/macrogrok-harness/blob/main/LICENSE.tri
library_name: custom
tags:
- code
- sovereign-compute
- snapkitty
- compilers
- python
---

> Source: [github.com/SNAPKITTYWEST/macrogrok-harness](https://github.com/SNAPKITTYWEST/macrogrok-harness)

# macrogrok-harness

Rust harness for **MACROGROK** fixed-point macro-models and **Transformer-X CUDA** (Tensor Core WMMA, attention memory, Tavily RAG).

## Architecture

* **Rust host** (`tokio`, `cudarc 0.11`, `reqwest`, `half`) + **CUDA** `cudarc` / custom kernels
* **Attention** β€” Tensor Core `WMMA 16x16x16` (`mma.sync` path available), explicit `AttentionMemory` KV-cache (contiguous/paged), FlashAttention online-softmax tiling
* **MACROGROK** β€” `Q1.14`/`Q3.12` fixed-point INFER4 harness (`src/macrogrok/`)
* **Tavily** β€” async HTTP RAG (`POST https://api.tavily.com/search`, `search_depth=advanced`)
* **X algorithm** β€” sparse/hybrid attention hook inside `attention_tc.cu` (insert sparsity mask / linear state / IndexPool before/after WMMA)

## Layout

```
Cargo.toml
src/
  main.rs              β€” tokio main, Transformer load, AttentionMemory, Tavily loop
  memory.rs            β€” AttentionMemory {k_cache, v_cache} [layers, heads, max_seq, head_dim]
  attention.rs         β€” TensorCoreAttention {forward()} (WMMA)
  transformer.rs       β€” TransformerConfig / Block / forward
  tavily.rs            β€” TavilyClient::search()
  macrogrok/mod.rs     β€” Q-format INFER4 Rust port (mirrors MACROGROK/src/sim.py)
cuda/
  attention_tc.cu      β€” WMMA kernel (QK^T + softmax + PV, head_dim tiling, smem)
  tma_mbarrier.ptx     β€” Hopper TMA + mbarrier bulk tensor (2D shared::cluster)
src/macrogrok/tests    β€” fixed-point vectors
```

## Build

```toml
# Cargo.toml excerpt
[dependencies]
cudarc = { version = "0.11", features = ["cuda-12000"] }
anyhow = "1"
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0.12", features = ["json"] }
serde = { version = "1", features = ["derive"] }
half = "2"
bytemuck = "1"
```

```bash
cargo build --release
cargo test
# CUDA kernels: nvcc -arch=sm_80 -ptx cuda/attention_tc.cu -o attention_tc.ptx
# Run
TAVILY_API_KEY=... cargo run --release -- --prompt "hello"
python -m pytest # if Python harness tests
```

## MACROGROK Harness

```rust
use macrogrok_harness::macrogrok::{infer4, Q1_14};
let input = [Q1_14(16384), Q1_14(0), Q1_14(0), Q1_14(0)];
let (output, score, flags) = infer4(input); // Q1.14, Q3.12, flags
```

Matches `MACROGROK/examples/infer4.asm` semantics: `Q1.14*Q1.14>>4` accum `Q3.24` β†’ `>>12` β†’ `+BIAS_Q3_12` β†’ `SAT` β†’ `THRESHOLD` β†’ `UPDATE_STATE_3_4` `(3*STATE+TARGET)/4`.

## Transformer-X Loop

```
tokenize β†’ Transformer::forward (QKV proj GEMM β†’ TensorCoreAttention::forward β†’ MLP)
         β†’ if tool call JSON β†’ TavilyClient::search β†’ inject β†’ continue
         β†’ AttentionMemory grows (reset on overflow)
```

## CUDA Notes

* `half` inputs, `float` accumulator, `scale=1/sqrt(head_dim)`, causal mask, `__shfl_xor` max/sum reductions, `__float2half` store via `smem_out` (`wmma::store_matrix_sync` β†’ shared β†’ `half`).
* Head-dim tiling required for `head_dim>16` (64/128): outer `d_tile` loops for `QK^T` reduction and `AV`.
* Hopper TMA: `cp.async.bulk.tensor.2d.shared::cluster.global.mbarrier::complete_tx::bytes` + `mbarrier.init/arrive.expect_tx/try_wait` (see `cuda/tma_mbarrier.ptx`). On Hopper `cuda::memcpy_async` lowers to TMA when `16-byte` aligned + barrier in SMEM.

## License

BSL-1.1 / AGPL-3.0 / MPL-2.0 β€” Patent Pending, BEL ESPRIT D ACCORD TRUST HOLDINGS INC.

### πŸ’Ό Commercial License

Snapkitty code is free and open under **AGPL-3.0** for open-source use. Building a commercial product or service? A **proprietary commercial license** from Snapkitty Collective LLC lets you ship this code without the AGPL's source-sharing and network-use obligations.

**[β†’ Get a commercial license](mailto:A.parr@belespritdaccord.uk?subject=Commercial%20license:%20macrogrok-harness)** Β· A.parr@belespritdaccord.uk