# Benchmark Guide & Operating Thresholds

This document covers how our benchmarks are timed, how accuracy is checked, and the exact size thresholds where Cortivra-Apex runs custom GPU kernels versus where it steps aside and delegates to standard PyTorch.

## 1. How Timings Are Measured

1. **Unmodified PyTorch Baselines**:
   Before any baseline timing runs (`F.rms_norm`, `torch.softmax`, `F.scaled_dot_product_attention`, `F.grid_sample`, `F.conv2d`), Cortivra's runtime auto-hook is disabled (`apex.disable_auto_intercept()`). All baseline numbers come directly from native PyTorch C++, cuBLAS, cuDNN, or FlashAttention-2.
2. **CUDA Event Hardware Timers**:
   Kernel execution time is recorded on the GPU stream using `torch.cuda.Event(enable_timing=True)` after warmup iterations and `torch.cuda.synchronize()` barriers.
3. **Warm-L2 and Cold-L2 (`128 MB` Flush) Modes**:
   - In multi-layer models, layer outputs remain in the GPU's on-chip L2 cache between consecutive layers (Warm-L2).
   - To measure cold off-chip DRAM bandwidth (Cold-L2), `verify_independently.py` zeros a separate `128 MB` GPU buffer (twice the size of the RTX 5080's `64 MB` L2 cache) before each timed iteration so every input byte must be read from main GPU memory.
4. **64-Bit Reference Accuracy Checks**:
   Outputs are compared against 64-bit `float64` reference calculations using cosine similarity and finiteness checks (`torch.isfinite`).

## 2. Two Attention Modes

Cortivra-Apex provides two attention modes depending on whether you are running an unmodified pretrained checkpoint or a long-context workload:

| Mode | How to Enable | Behavior | Recommended Use |
| :--- | :--- | :--- | :--- |
| **Exact Standard Attention** | `apex.enable_auto_intercept(wave_attention=False)` | Keeps `F.scaled_dot_product_attention` on standard softmax attention while accelerating `Linear`/`MatMul`, `4-Bit/8-Bit Weight Compression` (`7.10x`), `RMSNorm`, and `Softmax`. | Frozen pretrained checkpoints (Llama, Qwen, Mistral) where exact token-for-token softmax parity is required without fine-tuning. |
| **Fast Long-Context Attention** | `apex.enable_auto_intercept(wave_attention=True)` or `apex.wave_attention(q, k, v)` | Uses linear-time long-context attention (`O(N)` work and `0.00 MB` extra attention matrix memory) instead of quadratic `O(N^2)` pairwise attention. | Long-context processing (`8K–128K+` tokens), custom architectures, or models trained/fine-tuned with fast attention enabled. |

## 3. When Cortivra Steps Aside (Automatic Size Thresholds)

Custom GPU kernels are not faster on every input shape. On very small tensors, launch overhead outweighs kernel savings. Cortivra checks tensor dimensions before dispatch and routes small shapes directly to native PyTorch:

- **Matrix Multiplication (`M < 32`, `N < 32`, or `K < 16`)**: Handled directly by native PyTorch / cuBLAS.
- **Normalization (`Cols < 64` or `Total Elements < 256`)**: Handled directly by native PyTorch `F.rms_norm`.
- **Attention (`SeqLen < 32` or `HeadDim < 16`)**: Handled directly by native PyTorch `F.scaled_dot_product_attention`.
- **Batch-Size Routing in 4-Bit & 8-Bit Compressed Layers**:
  - For small batch sizes (`Batch * SeqLen <= 128`, such as single-user token generation), Cortivra decodes weights directly inside GPU registers during the matrix multiply so uncompressed weights are never written to VRAM.
  - For large batch sizes (`Batch * SeqLen > 128`, such as large-prompt prefill), Cortivra uses temporary per-layer unpacking into Tensor Core matrix multiply so large-batch prefill runs at full Tensor Core speed while permanent model weights remain compressed `7.10x` in VRAM.
- **Automatic Fallback on Error**:
  If any GPU driver error or `NaN`/`Inf` output is detected during a call, Cortivra automatically re-runs that operation on standard PyTorch so the application does not crash.

## 4. System Safety & Privacy

- **Standard User Mode Only**: `Cortivra-Apex.exe` runs with normal user permissions (`asInvoker`), never asks for Administrator access, installs no kernel drivers, and does not alter GPU clocks, voltages, or firmware.
- **Anti-Cheat Safe**: Does not inject DLLs or hook into external game processes.
- **0.0 MB Idle Memory**: Releases all GPU buffers (`0.0 MB` VRAM) when paused or idle.
- **Offline Operation**: Runs locally (`127.0.0.1:7860`) and sends no telemetry, prompts, or model weights over the network.
