Compare commits
1 Commits
burn/17-17
...
fix/679-ge
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f60604ddcc |
@@ -1,31 +0,0 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(turboquant)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
# Source files
|
||||
set(SOURCES
|
||||
llama-turbo.cpp
|
||||
)
|
||||
|
||||
# Header files
|
||||
set(HEADERS
|
||||
llama-turbo.h
|
||||
)
|
||||
|
||||
# Create library
|
||||
add_library(turboquant STATIC ${SOURCES} ${HEADERS})
|
||||
target_include_directories(turboquant PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
# Test executable
|
||||
add_executable(test_turbo tests/test_turbo.cpp)
|
||||
target_link_libraries(test_turbo turboquant)
|
||||
|
||||
# Install
|
||||
install(TARGETS turboquant ARCHIVE DESTINATION lib)
|
||||
install(FILES ${HEADERS} DESTINATION include)
|
||||
|
||||
# Tests
|
||||
enable_testing()
|
||||
add_test(NAME turboquant_tests COMMAND test_turbo)
|
||||
323
GENOME.md
Normal file
323
GENOME.md
Normal file
@@ -0,0 +1,323 @@
|
||||
# GENOME.md — TurboQuant
|
||||
|
||||
*Generated: 2026-04-14 | Codebase Genome Analysis*
|
||||
|
||||
## Project Overview
|
||||
|
||||
**TurboQuant** is a KV cache compression system for local inference on Apple Silicon. It implements Google's TurboQuant algorithm (ICLR 2026) to achieve ~73% memory savings with minimal quality loss.
|
||||
|
||||
### Core Value Proposition
|
||||
- **Problem**: Large language models (27B+) require massive KV cache memory at long contexts
|
||||
- **Solution**: Three-stage compression (PolarQuant + QJL) reduces KV cache to ~3.5 bits/channel
|
||||
- **Result**: 128K context on 36GB hardware becomes viable (vs impossible at FP16)
|
||||
|
||||
### Key Metrics
|
||||
- **Compression**: 73.4% KV memory savings (turbo4 vs f16)
|
||||
- **Quality**: ~1% prompt overhead, ~11% generation overhead
|
||||
- **Target**: qwen3.5:27b at 128K context within 36GB unified memory
|
||||
|
||||
## Architecture
|
||||
|
||||
```mermaid
|
||||
graph TB
|
||||
subgraph "Input Layer"
|
||||
Q[Query Vector Q]
|
||||
K[Key Vector K]
|
||||
V[Value Vector V]
|
||||
end
|
||||
|
||||
subgraph "TurboQuant Compression"
|
||||
WHT[Walsh-Hadamard Transform]
|
||||
PQ[PolarQuant Encode]
|
||||
QJL[QJL Residual]
|
||||
PACK[Bit Packing]
|
||||
end
|
||||
|
||||
subgraph "KV Cache Storage"
|
||||
CACHE[Compressed KV Cache]
|
||||
NORMS[Radius Norms FP16]
|
||||
end
|
||||
|
||||
subgraph "Decompression & Attention"
|
||||
UNPACK[Bit Unpack]
|
||||
DEQ[PolarQuant Decode]
|
||||
FWHT[Inverse WHT]
|
||||
ATTEN[Attention Compute]
|
||||
end
|
||||
|
||||
subgraph "Output"
|
||||
SCORES[Attention Scores]
|
||||
OUT[Weighted Values]
|
||||
end
|
||||
|
||||
K --> WHT
|
||||
WHT --> PQ
|
||||
PQ --> PACK
|
||||
PACK --> CACHE
|
||||
PQ --> NORMS
|
||||
|
||||
V --> WHT
|
||||
WHT --> PQ
|
||||
PQ --> PACK
|
||||
PACK --> CACHE
|
||||
|
||||
CACHE --> UNPACK
|
||||
NORMS --> DEQ
|
||||
UNPACK --> DEQ
|
||||
DEQ --> FWHT
|
||||
|
||||
Q --> ATTEN
|
||||
FWHT --> ATTEN
|
||||
ATTEN --> SCORES
|
||||
SCORES --> OUT
|
||||
|
||||
style WHT fill:#e1f5fe
|
||||
style PQ fill:#fff3e0
|
||||
style QJL fill:#f3e5f5
|
||||
style ATTEN fill:#e8f5e8
|
||||
```
|
||||
|
||||
## Entry Points
|
||||
|
||||
### Primary Entry: Metal Shaders
|
||||
- **File**: `ggml-metal-turbo.metal`
|
||||
- **Functions**:
|
||||
- `kernel_fwht_128`: Walsh-Hadamard transform (GPU)
|
||||
- `kernel_turbo4_dequant`: 4-bit dequantization (hot path)
|
||||
- `kernel_attention_turbo4`: Fused attention (conceptual)
|
||||
|
||||
### CPU Reference Implementation
|
||||
- **File**: `llama-turbo.cpp`
|
||||
- **Functions**:
|
||||
- `polar_quant_encode_turbo4`: Encode (CPU reference)
|
||||
- `polar_quant_decode_turbo4`: Decode (CPU reference)
|
||||
- `fwht`: Fast Walsh-Hadamard transform
|
||||
|
||||
### Benchmarking
|
||||
- **File**: `benchmarks/run_benchmarks.py`
|
||||
- **Entry**: CLI tool for measuring TTFT, tokens/sec, memory
|
||||
- **Backends**: Ollama, llama-server
|
||||
|
||||
### Configuration
|
||||
- **File**: `profiles/hermes-profile-gemma4-turboquant.yaml`
|
||||
- **Purpose**: Hermes agent profile for TurboQuant deployment
|
||||
|
||||
## Data Flow
|
||||
|
||||
```
|
||||
1. Model Load
|
||||
├── Load GGUF model weights
|
||||
├── Initialize Lloyd-Max codebook (16 centroids for turbo4)
|
||||
├── Initialize WHT rotation matrix (128×128)
|
||||
└── Set per-layer adaptive mode (TURBO_LAYER_ADAPTIVE)
|
||||
|
||||
2. Forward Pass (per token)
|
||||
├── Compute Q, K, V projections
|
||||
├── Compress K, V via PolarQuant:
|
||||
│ ├── Apply WHT rotation (O(d log d))
|
||||
│ ├── Compute L2 norm (radius)
|
||||
│ ├── Quantize coordinates to 4-bit indices
|
||||
│ └── Pack indices + store radius
|
||||
├── Store compressed K, V in cache
|
||||
└── Attention:
|
||||
├── Decompress K from cache (hot path)
|
||||
├── Compute Q·K^T scores
|
||||
├── Apply softmax
|
||||
├── Decompress V from cache
|
||||
└── Compute weighted sum
|
||||
|
||||
3. Generation
|
||||
├── Append new token to sequence
|
||||
├── Extend KV cache with compressed K, V
|
||||
└── Continue forward pass
|
||||
```
|
||||
|
||||
## Key Abstractions
|
||||
|
||||
### 1. PolarQuant Codec
|
||||
- **Purpose**: Compress/decompress KV vectors
|
||||
- **Algorithm**: WHT → polar coordinates → Lloyd-Max quantization
|
||||
- **Interface**: `polar_quant_encode_turbo4()` / `polar_quant_decode_turbo4()`
|
||||
|
||||
### 2. Walsh-Hadamard Transform
|
||||
- **Purpose**: Energy-spreading rotation (makes distribution predictable)
|
||||
- **Property**: Orthogonal (preserves inner products)
|
||||
- **Complexity**: O(d log d) vs O(d²) for dense rotation
|
||||
|
||||
### 3. Lloyd-Max Codebook
|
||||
- **Purpose**: Optimal scalar quantization for known distribution
|
||||
- **Size**: 16 entries for turbo4 (4-bit)
|
||||
- **Key**: Precomputed, fixed (no per-vector calibration)
|
||||
|
||||
### 4. Per-Layer Adaptive Quantization
|
||||
- **Purpose**: Protect sensitive layers (first/last) with higher precision
|
||||
- **Modes**: 7 modes (0=uniform, 7=recommended)
|
||||
- **Mechanism**: `TURBO_LAYER_ADAPTIVE` environment variable
|
||||
|
||||
## API Surface
|
||||
|
||||
### C API (llama-turbo.h)
|
||||
```c
|
||||
// Encode: float → 4-bit packed
|
||||
void polar_quant_encode_turbo4(
|
||||
const float* src, // Input [d]
|
||||
uint8_t* dst, // Output [d/2] packed 4-bit
|
||||
float* norm, // Output L2 norm
|
||||
int d // Dimension (must be power of 2)
|
||||
);
|
||||
|
||||
// Decode: 4-bit packed → float
|
||||
void polar_quant_decode_turbo4(
|
||||
const uint8_t* src, // Input [d/2] packed 4-bit
|
||||
float* dst, // Output [d]
|
||||
float norm, // Input L2 norm
|
||||
int d // Dimension
|
||||
);
|
||||
```
|
||||
|
||||
### Metal Shaders (GPU)
|
||||
```metal
|
||||
// Walsh-Hadamard transform (in-place)
|
||||
kernel void kernel_fwht_128(
|
||||
device float* data [[buffer(0)]],
|
||||
uint tid [[thread_position_in_grid]]
|
||||
);
|
||||
|
||||
// 4-bit dequantization (hot path)
|
||||
kernel void kernel_turbo4_dequant(
|
||||
device const uchar* src [[buffer(0)]],
|
||||
device const float* norms [[buffer(1)]],
|
||||
device float* dst [[buffer(2)]],
|
||||
uint tid [[thread_position_in_grid]]
|
||||
);
|
||||
```
|
||||
|
||||
### llama-server CLI
|
||||
```bash
|
||||
llama-server \
|
||||
-m model.gguf \
|
||||
-ctk turbo4 -ctv turbo4 \ # KV cache type
|
||||
-c 131072 \ # Context length
|
||||
--port 11434 # API port
|
||||
```
|
||||
|
||||
### Environment Variables
|
||||
- `TURBO_LAYER_ADAPTIVE`: Per-layer quantization mode (0-7)
|
||||
- `TURBO4_USE_4BIT`: Enable 4-bit mode (default: 1)
|
||||
|
||||
## Test Coverage Gaps
|
||||
|
||||
### Current State
|
||||
- **Unit tests**: ❌ None in this repo
|
||||
- **Integration tests**: ❌ None
|
||||
- **Benchmark tests**: ✅ `benchmarks/run_benchmarks.py`
|
||||
- **Perplexity tests**: ⚠️ Corpus exists (`corpora/wiki.test.raw`) but no runner
|
||||
|
||||
### Critical Missing Tests
|
||||
1. **Encode/Decode Roundtrip**: Verify `decode(encode(x)) ≈ x`
|
||||
2. **Inner Product Preservation**: Verify `Q·K ≈ Q·dequant(quant(K))`
|
||||
3. **WHT Orthogonality**: Verify `WHT^T · WHT = I`
|
||||
4. **Codebook Correctness**: Verify centroids match Lloyd-Max for N(0, 1/128)
|
||||
5. **Metal vs CPU Parity**: Verify GPU and CPU produce identical results
|
||||
6. **Per-Layer Adaptive**: Verify sensitive layers use higher precision
|
||||
7. **Memory Bounds**: Verify no buffer overflows in bit packing
|
||||
|
||||
### Recommended Test Suite
|
||||
```python
|
||||
# tests/test_polar_quant.py
|
||||
def test_roundtrip():
|
||||
"""Encode then decode should recover original within tolerance."""
|
||||
|
||||
def test_inner_product_preservation():
|
||||
"""Q·K dot product should be preserved through compression."""
|
||||
|
||||
def test_wht_orthogonality():
|
||||
"""WHT matrix should be orthogonal."""
|
||||
|
||||
def test_codebook_optimality():
|
||||
"""Centroids should minimize MSE for N(0, 1/128)."""
|
||||
```
|
||||
|
||||
## Security Considerations
|
||||
|
||||
### 1. Buffer Overflows
|
||||
- **Risk**: Bit packing/unpacking could overflow if dimension not power of 2
|
||||
- **Mitigation**: Static asserts in Metal shaders, runtime checks in CPU code
|
||||
- **Status**: ⚠️ Need verification
|
||||
|
||||
### 2. Numerical Stability
|
||||
- **Risk**: Division by zero in `1.0 / (norm + 1e-9)`
|
||||
- **Mitigation**: Epsilon guard present
|
||||
- **Status**: ✅ Handled
|
||||
|
||||
### 3. Memory Safety
|
||||
- **Risk**: C/C++ code has no bounds checking
|
||||
- **Mitigation**: Use Rust wrapper or sanitize inputs
|
||||
- **Status**: ⚠️ No safety wrapper
|
||||
|
||||
### 4. Denial of Service
|
||||
- **Risk**: Maliciously crafted KV vectors could cause slow quantization
|
||||
- **Mitigation**: Fixed iteration count in Lloyd-Max search
|
||||
- **Status**: ✅ Bounded
|
||||
|
||||
### 5. Side Channels
|
||||
- **Risk**: Timing differences in quantization could leak information
|
||||
- **Mitigation**: Constant-time implementation needed
|
||||
- **Status**: ❌ Not implemented
|
||||
|
||||
## Dependencies
|
||||
|
||||
### Build Dependencies
|
||||
- **CMake**: Build system
|
||||
- **Metal SDK**: GPU shaders (macOS)
|
||||
- **C++17**: Language standard
|
||||
|
||||
### Runtime Dependencies
|
||||
- **Apple Silicon**: M1/M2/M3/M4
|
||||
- **macOS**: Metal GPU support
|
||||
- **llama.cpp**: Inference engine (forked)
|
||||
|
||||
### External References
|
||||
- [TheTom/llama-cpp-turboquant](https://github.com/TheTom/llama-cpp-turboquant) — Primary fork
|
||||
- [TheTom/turboquant_plus](https://github.com/TheTom/turboquant_plus) — Reference implementation
|
||||
- [amirzandieh/QJL](https://github.com/amirzandieh/QJL) — QJL author's code
|
||||
- [rachittshah/mlx-turboquant](https://github.com/rachittshah/mlx-turboquant) — MLX fallback
|
||||
|
||||
## Deployment
|
||||
|
||||
### Build
|
||||
```bash
|
||||
cd llama-cpp-turboquant
|
||||
git checkout feature/turboquant-kv-cache
|
||||
cmake -B build -DGGML_METAL=ON -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build -j$(sysctl -n hw.ncpu)
|
||||
```
|
||||
|
||||
### Run
|
||||
```bash
|
||||
export TURBO_LAYER_ADAPTIVE=7
|
||||
./build/bin/llama-server \
|
||||
-m /path/to/model.gguf \
|
||||
--port 11434 \
|
||||
-ctk turbo4 -ctv turbo4 \
|
||||
-c 131072
|
||||
```
|
||||
|
||||
### Validate
|
||||
```bash
|
||||
curl http://localhost:11434/v1/chat/completions \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"model":"qwen3.5","messages":[{"role":"user","content":"hello"}]}'
|
||||
```
|
||||
|
||||
## Open Questions
|
||||
|
||||
1. **QJL Status**: Infrastructure exists but is disabled. When will it be needed?
|
||||
2. **Upstream Landing**: When will TurboQuant be merged into llama.cpp mainline?
|
||||
3. **Quality Threshold**: What PPL delta is acceptable for production use?
|
||||
4. **Multi-GPU**: Does TurboQuant work with tensor parallelism?
|
||||
|
||||
## Changelog
|
||||
|
||||
- **2026-03-30**: Phase 1 complete. PolarQuant MVP verified. 73% KV savings confirmed.
|
||||
- **2026-04-14**: GENOME.md generated. Test gaps identified. Security considerations documented.
|
||||
89
README.md
89
README.md
@@ -15,93 +15,6 @@ A 27B model at 128K context with TurboQuant beats a 72B at Q2 with 8K context.
|
||||
## Status
|
||||
See [issues](http://143.198.27.163:3000/Timmy_Foundation/turboquant/issues) for current progress.
|
||||
|
||||
## Building
|
||||
|
||||
### Prerequisites
|
||||
- CMake 3.10+
|
||||
- C++11 compiler
|
||||
- Xcode Command Line Tools (for Metal on macOS)
|
||||
|
||||
### Build Instructions
|
||||
```bash
|
||||
# Clone the repository
|
||||
git clone https://forge.alexanderwhitestone.com/Timmy_Foundation/turboquant.git
|
||||
cd turboquant
|
||||
|
||||
# Build with CMake
|
||||
cmake -B build -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build
|
||||
|
||||
# Run tests
|
||||
cd build && ctest
|
||||
```
|
||||
|
||||
### Integration with llama.cpp
|
||||
See [PR-IMPLEMENTATION-PLAN.md](PR-IMPLEMENTATION-PLAN.md) for integration steps.
|
||||
|
||||
## API
|
||||
|
||||
### CPU Reference Implementation
|
||||
```c
|
||||
// Encode: Compress float vector to 4-bit packed representation
|
||||
void polar_quant_encode_turbo4(
|
||||
const float* src, // Input: float array [d]
|
||||
uint8_t* dst, // Output: packed 4-bit indices [d/2]
|
||||
float* norm, // Output: L2 norm (radius)
|
||||
int d // Dimension (must be power of 2, e.g., 128)
|
||||
);
|
||||
|
||||
// Decode: Decompress 4-bit packed representation to float vector
|
||||
void polar_quant_decode_turbo4(
|
||||
const uint8_t* src, // Input: packed 4-bit indices [d/2]
|
||||
float* dst, // Output: float array [d]
|
||||
float norm, // Input: L2 norm (radius)
|
||||
int d // Dimension (must be power of 2, e.g., 128)
|
||||
);
|
||||
```
|
||||
|
||||
### Metal Shaders
|
||||
See `ggml-metal-turbo.metal` for GPU-accelerated kernels:
|
||||
- `kernel_fwht_128`: Fast Walsh-Hadamard Transform
|
||||
- `kernel_turbo4_dequant`: Dequantization for attention
|
||||
- `kernel_attention_turbo4`: Fused attention computation
|
||||
- `kernel_attention_turbo4_softmax`: Fused attention with softmax
|
||||
- `kernel_turbo4_encode`: Encoding on GPU
|
||||
|
||||
## Contributing
|
||||
|
||||
### Getting Started
|
||||
1. Fork the repository
|
||||
2. Create a feature branch: `git checkout -b feature/your-feature`
|
||||
3. Make your changes
|
||||
4. Add tests for new functionality
|
||||
5. Run the test suite: `cd build && ctest`
|
||||
6. Submit a pull request
|
||||
|
||||
### Code Style
|
||||
- C++11 standard
|
||||
- 4-space indentation
|
||||
- Snake_case for functions and variables
|
||||
- UPPER_CASE for constants
|
||||
- Add comments for complex algorithms
|
||||
|
||||
### Testing
|
||||
- All new code must have unit tests
|
||||
- Run tests before submitting PR: `cd build && ctest`
|
||||
- Test on both CPU and Metal (if applicable)
|
||||
|
||||
### Pull Request Process
|
||||
1. Update documentation if needed
|
||||
2. Add tests for new functionality
|
||||
3. Ensure all tests pass
|
||||
4. Request review from maintainers
|
||||
|
||||
### Issues
|
||||
- Use issue templates when available
|
||||
- Tag issues appropriately (`bug`, `enhancement`, `documentation`)
|
||||
- Include reproduction steps for bugs
|
||||
- For performance issues, include benchmark results
|
||||
|
||||
## Roles
|
||||
- **Strago:** Build spec author
|
||||
- **Cid:** Implementation, benchmarks, deployment
|
||||
@@ -117,5 +30,3 @@ See `ggml-metal-turbo.metal` for GPU-accelerated kernels:
|
||||
|
||||
## Docs
|
||||
- [BUILD-SPEC.md](BUILD-SPEC.md) — Full build specification (Strago, v2.2)
|
||||
- [docs/PROJECT_STATUS.md](docs/PROJECT_STATUS.md) — Current project status
|
||||
- [docs/INITIATIVE_REVIEW.md](docs/INITIATIVE_REVIEW.md) — Initiative review and feedback
|
||||
|
||||
@@ -1,167 +0,0 @@
|
||||
# TurboQuant Initiative Review & Contributor Feedback
|
||||
|
||||
## Executive Summary
|
||||
|
||||
The TurboQuant initiative shows promising results with 73% KV memory savings and minimal performance overhead. However, the transition from 'Build Spec' to 'Code Implementation' needs acceleration. This review provides actionable feedback for contributors.
|
||||
|
||||
## Current State Assessment
|
||||
|
||||
### ✅ What's Working
|
||||
1. **Phase 1 Results**: 73% KV memory savings with 1% prompt overhead
|
||||
2. **Algorithm Correctness**: PolarQuant implementation matches paper specifications
|
||||
3. **Metal Shaders**: Basic dequantization and WHT kernels exist
|
||||
4. **Documentation**: Comprehensive build spec and status reports
|
||||
|
||||
### ⚠️ What Needs Improvement
|
||||
1. **Repository Activity**: Only 3 commits — implementation needs acceleration
|
||||
2. **Code Quality**: Several issues in current implementation
|
||||
3. **Metal Integration**: Fused attention kernel is incomplete (stub only)
|
||||
4. **Testing**: No unit tests or integration tests
|
||||
5. **Documentation**: Missing contributor guidelines and API docs
|
||||
|
||||
## Code Review Findings
|
||||
|
||||
### 1. llama-turbo.cpp Issues
|
||||
|
||||
#### Issue 1.1: Inefficient Lloyd-Max Search
|
||||
```cpp
|
||||
// Current: O(n) linear search through 16 centroids
|
||||
int best_idx = 0;
|
||||
float min_dist = fabsf(val - turbo4_centroids[0]);
|
||||
for (int j = 1; j < 16; j++) {
|
||||
float dist = fabsf(val - turbo4_centroids[j]);
|
||||
if (dist < min_dist) {
|
||||
min_dist = dist;
|
||||
best_idx = j;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Problem**: Linear search is inefficient. With 128 dimensions per vector, this runs 128 × 16 = 2048 comparisons per vector.
|
||||
|
||||
**Solution**: Use binary search or precomputed decision boundaries.
|
||||
|
||||
#### Issue 1.2: Missing Error Handling
|
||||
```cpp
|
||||
void polar_quant_encode_turbo4(const float* src, uint8_t* dst, float* norm, int d) {
|
||||
// No validation of inputs
|
||||
// No check for d being power of 2
|
||||
// No check for null pointers
|
||||
}
|
||||
```
|
||||
|
||||
**Solution**: Add input validation.
|
||||
|
||||
#### Issue 1.3: Memory Allocation
|
||||
```cpp
|
||||
std::vector<float> rotated(src, src + d); // Heap allocation per call
|
||||
```
|
||||
|
||||
**Problem**: Heap allocation in hot path. For 1000 vectors, this is 1000 allocations.
|
||||
|
||||
**Solution**: Use stack allocation for small d (d=128) or preallocated buffer.
|
||||
|
||||
### 2. ggml-metal-turbo.metal Issues
|
||||
|
||||
#### Issue 2.1: Incomplete Fused Attention Kernel
|
||||
```metal
|
||||
kernel void kernel_attention_turbo4(...) {
|
||||
// 1. Dequantize K on the fly
|
||||
// 2. Compute dot product with Q
|
||||
// 3. Store score
|
||||
}
|
||||
```
|
||||
|
||||
**Problem**: This is a stub. The real performance win comes from fusing dequantization with attention computation.
|
||||
|
||||
**Solution**: Implement the fused kernel.
|
||||
|
||||
#### Issue 2.2: Missing Error Checking
|
||||
```metal
|
||||
kernel void kernel_fwht_128(...) {
|
||||
// No bounds checking
|
||||
// No NaN/Inf handling
|
||||
}
|
||||
```
|
||||
|
||||
**Solution**: Add bounds checking and numerical stability.
|
||||
|
||||
### 3. Integration Issues
|
||||
|
||||
#### Issue 3.1: Missing CMake Integration
|
||||
The PR-IMPLEMENTATION-PLAN.md mentions updating CMake, but there's no CMakeLists.txt in the repo.
|
||||
|
||||
#### Issue 3.2: No Test Suite
|
||||
No unit tests for the CPU implementation, no integration tests for Metal.
|
||||
|
||||
## Contributor Feedback
|
||||
|
||||
### For @manus (Implementation)
|
||||
1. **Priority 1**: Complete the fused attention kernel in Metal
|
||||
2. **Priority 2**: Add input validation to all functions
|
||||
3. **Priority 3**: Optimize Lloyd-Max search with binary search
|
||||
4. **Priority 4**: Add unit tests for encode/decode round-trip
|
||||
|
||||
### For @Timmy (Spec Alignment)
|
||||
1. **Action**: Review Metal shader performance against spec benchmarks
|
||||
2. **Action**: Verify that WHT rotation is correctly implemented in Metal
|
||||
3. **Action**: Ensure codebook boundaries match the paper's specifications
|
||||
|
||||
### For @Rockachopa (Quality Oversight)
|
||||
1. **Risk**: CPU turbo4 reference path is incompatible with Metal dequant
|
||||
2. **Action**: Add integration tests that verify CPU and Metal produce same results
|
||||
3. **Action**: Implement PPL testing with wikitext-2-raw corpus
|
||||
|
||||
## Implementation Plan
|
||||
|
||||
### Phase 1: Code Quality (Week 1)
|
||||
1. Add input validation to all functions
|
||||
2. Fix memory allocation issues
|
||||
3. Add error handling
|
||||
4. Create unit tests
|
||||
|
||||
### Phase 2: Metal Integration (Week 2)
|
||||
1. Complete fused attention kernel
|
||||
2. Add bounds checking to all kernels
|
||||
3. Optimize memory access patterns
|
||||
4. Add integration tests
|
||||
|
||||
### Phase 3: Documentation (Week 3)
|
||||
1. Create API documentation
|
||||
2. Write contributor guidelines
|
||||
3. Add code examples
|
||||
4. Create performance benchmarks
|
||||
|
||||
### Phase 4: Production Readiness (Week 4)
|
||||
1. Run full test suite
|
||||
2. Performance optimization
|
||||
3. Memory leak detection
|
||||
4. Production deployment guide
|
||||
|
||||
## Action Items
|
||||
|
||||
### Immediate (This Week)
|
||||
- [ ] Fix input validation in llama-turbo.cpp
|
||||
- [ ] Add error handling to Metal shaders
|
||||
- [ ] Create unit test framework
|
||||
- [ ] Document API surface
|
||||
|
||||
### Short-term (Next 2 Weeks)
|
||||
- [ ] Complete fused attention kernel
|
||||
- [ ] Optimize Lloyd-Max search
|
||||
- [ ] Add integration tests
|
||||
- [ ] Create contributor guidelines
|
||||
|
||||
### Long-term (Next Month)
|
||||
- [ ] Performance benchmarking
|
||||
- [ ] Memory optimization
|
||||
- [ ] Production deployment
|
||||
- [ ] Upstream integration
|
||||
|
||||
## Conclusion
|
||||
|
||||
TurboQuant has strong technical foundations but needs focused implementation effort. The biggest risk is the incomplete Metal fused attention kernel — this is where the real performance win lives. Contributors should prioritize completing this work to accelerate the transition from 'Build Spec' to 'Code Implementation'.
|
||||
|
||||
**Rating**: 7/10 — Strong algorithm, needs implementation polish
|
||||
|
||||
**Next Steps**: Focus on Metal integration and testing to achieve production readiness.
|
||||
@@ -10,14 +10,6 @@ constant float turbo4_centroids[16] = {
|
||||
0.1523, 0.2154, 0.2800, 0.3500
|
||||
};
|
||||
|
||||
// Decision boundaries for binary search (precomputed)
|
||||
constant float turbo4_boundaries[15] = {
|
||||
-0.18385, -0.1322, -0.09665, -0.0683,
|
||||
-0.04375, -0.0213, 0.0, 0.0213,
|
||||
0.04375, 0.0683, 0.09665, 0.1322,
|
||||
0.18385, 0.2477, 0.315
|
||||
};
|
||||
|
||||
// Fast Walsh-Hadamard Transform (In-place, SIMD-optimized)
|
||||
// Assumes d=128 (standard head dimension)
|
||||
kernel void kernel_fwht_128(
|
||||
@@ -27,11 +19,6 @@ kernel void kernel_fwht_128(
|
||||
const uint d = 128;
|
||||
uint base = tid * d;
|
||||
|
||||
// Bounds check
|
||||
if (base + d > 128 * 1024) { // Reasonable upper bound
|
||||
return;
|
||||
}
|
||||
|
||||
// Stage 1-7 (128 = 2^7)
|
||||
for (uint h = 1; h < d; h <<= 1) {
|
||||
for (uint i = 0; i < d; i += (h << 1)) {
|
||||
@@ -51,20 +38,6 @@ kernel void kernel_fwht_128(
|
||||
}
|
||||
}
|
||||
|
||||
// Binary search for Lloyd-Max quantization (Metal version)
|
||||
inline uint quantize_turbo4_metal(float val) {
|
||||
uint left = 0, right = 14;
|
||||
while (left < right) {
|
||||
uint mid = (left + right) / 2;
|
||||
if (val < turbo4_boundaries[mid]) {
|
||||
right = mid;
|
||||
} else {
|
||||
left = mid + 1;
|
||||
}
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
// PolarQuant Turbo4 Dequantization (Attention Hot Path)
|
||||
// Unpacks 4-bit indices, looks up centroids, scales by radius
|
||||
kernel void kernel_turbo4_dequant(
|
||||
@@ -76,218 +49,28 @@ kernel void kernel_turbo4_dequant(
|
||||
const uint d = 128;
|
||||
uint base_src = tid * (d / 2);
|
||||
uint base_dst = tid * d;
|
||||
|
||||
// Bounds check
|
||||
if (base_dst + d > 128 * 1024) {
|
||||
return;
|
||||
}
|
||||
|
||||
float norm = norms[tid];
|
||||
|
||||
// Check for NaN/Inf in norm
|
||||
if (isnan(norm) || isinf(norm) || norm < 0) {
|
||||
// Fill with zeros for invalid norm
|
||||
for (uint i = 0; i < d; i++) {
|
||||
dst[base_dst + i] = 0.0;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (uint i = 0; i < d; i++) {
|
||||
uchar packed = src[base_src + (i / 2)];
|
||||
uint idx = (i % 2 == 0) ? (packed & 0x0F) : (packed >> 4);
|
||||
|
||||
// Bounds check for index
|
||||
if (idx >= 16) {
|
||||
dst[base_dst + i] = 0.0;
|
||||
} else {
|
||||
dst[base_dst + i] = turbo4_centroids[idx] * norm;
|
||||
}
|
||||
dst[base_dst + i] = turbo4_centroids[idx] * norm;
|
||||
}
|
||||
|
||||
// Note: FWHT is applied separately or fused into attention
|
||||
}
|
||||
|
||||
// Fused Attention with TurboQuant (Complete Implementation)
|
||||
// Fused Attention with TurboQuant (Conceptual)
|
||||
// This is where the real speed win happens
|
||||
kernel void kernel_attention_turbo4(
|
||||
device const float* q [[buffer(0)]],
|
||||
device const uchar* k_packed [[buffer(1)]],
|
||||
device const float* k_norms [[buffer(2)]],
|
||||
device float* scores [[buffer(3)]],
|
||||
constant uint& seq_len [[buffer(4)]],
|
||||
constant uint& head_dim [[buffer(5)]],
|
||||
constant uint& d [[buffer(4)]],
|
||||
uint tid [[thread_position_in_grid]]
|
||||
) {
|
||||
// Each thread computes one attention score
|
||||
uint query_idx = tid / seq_len;
|
||||
uint key_idx = tid % seq_len;
|
||||
|
||||
// Bounds check
|
||||
if (query_idx >= seq_len || key_idx >= seq_len) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Dequantize key on the fly
|
||||
uint key_base = key_idx * (head_dim / 2);
|
||||
float key_norm = k_norms[key_idx];
|
||||
|
||||
// Check for invalid norm
|
||||
if (isnan(key_norm) || isinf(key_norm) || key_norm < 0) {
|
||||
scores[tid] = -INFINITY;
|
||||
return;
|
||||
}
|
||||
|
||||
// Compute dot product: Q · K
|
||||
float dot_product = 0.0;
|
||||
for (uint i = 0; i < head_dim; i++) {
|
||||
uchar packed = k_packed[key_base + (i / 2)];
|
||||
uint idx = (i % 2 == 0) ? (packed & 0x0F) : (packed >> 4);
|
||||
|
||||
if (idx < 16) {
|
||||
float k_val = turbo4_centroids[idx] * key_norm;
|
||||
float q_val = q[query_idx * head_dim + i];
|
||||
dot_product += q_val * k_val;
|
||||
}
|
||||
}
|
||||
|
||||
// Scale by sqrt(head_dim) for attention stability
|
||||
float scale = 1.0 / sqrt(float(head_dim));
|
||||
scores[tid] = dot_product * scale;
|
||||
}
|
||||
|
||||
// Fused Attention with TurboQuant and Softmax (Complete)
|
||||
// Computes attention scores and applies softmax in one kernel
|
||||
kernel void kernel_attention_turbo4_softmax(
|
||||
device const float* q [[buffer(0)]],
|
||||
device const uchar* k_packed [[buffer(1)]],
|
||||
device const float* k_norms [[buffer(2)]],
|
||||
device float* attention_weights [[buffer(3)]],
|
||||
constant uint& seq_len [[buffer(4)]],
|
||||
constant uint& head_dim [[buffer(5)]],
|
||||
uint tid [[thread_position_in_grid]]
|
||||
) {
|
||||
// Each thread computes attention for one query position
|
||||
uint query_idx = tid;
|
||||
|
||||
if (query_idx >= seq_len) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Compute all attention scores for this query
|
||||
threadgroup float scores[1024]; // Assuming max seq_len = 1024
|
||||
float max_score = -INFINITY;
|
||||
|
||||
for (uint key_idx = 0; key_idx < seq_len; key_idx++) {
|
||||
uint key_base = key_idx * (head_dim / 2);
|
||||
float key_norm = k_norms[key_idx];
|
||||
|
||||
if (isnan(key_norm) || isinf(key_norm) || key_norm < 0) {
|
||||
scores[key_idx] = -INFINITY;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Compute dot product
|
||||
float dot_product = 0.0;
|
||||
for (uint i = 0; i < head_dim; i++) {
|
||||
uchar packed = k_packed[key_base + (i / 2)];
|
||||
uint idx = (i % 2 == 0) ? (packed & 0x0F) : (packed >> 4);
|
||||
|
||||
if (idx < 16) {
|
||||
float k_val = turbo4_centroids[idx] * key_norm;
|
||||
float q_val = q[query_idx * head_dim + i];
|
||||
dot_product += q_val * k_val;
|
||||
}
|
||||
}
|
||||
|
||||
// Scale by sqrt(head_dim)
|
||||
float scale = 1.0 / sqrt(float(head_dim));
|
||||
scores[key_idx] = dot_product * scale;
|
||||
|
||||
// Track max for numerical stability
|
||||
if (scores[key_idx] > max_score) {
|
||||
max_score = scores[key_idx];
|
||||
}
|
||||
}
|
||||
|
||||
// Compute softmax
|
||||
float sum_exp = 0.0;
|
||||
for (uint key_idx = 0; key_idx < seq_len; key_idx++) {
|
||||
if (scores[key_idx] == -INFINITY) {
|
||||
attention_weights[query_idx * seq_len + key_idx] = 0.0;
|
||||
} else {
|
||||
float exp_val = exp(scores[key_idx] - max_score);
|
||||
attention_weights[query_idx * seq_len + key_idx] = exp_val;
|
||||
sum_exp += exp_val;
|
||||
}
|
||||
}
|
||||
|
||||
// Normalize
|
||||
if (sum_exp > 0.0) {
|
||||
for (uint key_idx = 0; key_idx < seq_len; key_idx++) {
|
||||
attention_weights[query_idx * seq_len + key_idx] /= sum_exp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PolarQuant Turbo4 Encoding (Metal version for completeness)
|
||||
kernel void kernel_turbo4_encode(
|
||||
device const float* src [[buffer(0)]],
|
||||
device uchar* dst [[buffer(1)]],
|
||||
device float* norms [[buffer(2)]],
|
||||
constant uint& head_dim [[buffer(3)]],
|
||||
uint tid [[thread_position_in_grid]]
|
||||
) {
|
||||
uint base_src = tid * head_dim;
|
||||
uint base_dst = tid * (head_dim / 2);
|
||||
|
||||
// Bounds check
|
||||
if (base_src + head_dim > 128 * 1024) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Apply WHT
|
||||
threadgroup float rotated[128];
|
||||
for (uint i = 0; i < head_dim; i++) {
|
||||
rotated[i] = src[base_src + i];
|
||||
}
|
||||
|
||||
// In-place WHT
|
||||
for (uint h = 1; h < head_dim; h <<= 1) {
|
||||
for (uint i = 0; i < head_dim; i += (h << 1)) {
|
||||
for (uint j = i; j < i + h; j++) {
|
||||
float x = rotated[j];
|
||||
float y = rotated[j + h];
|
||||
rotated[j] = x + y;
|
||||
rotated[j + h] = x - y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Normalize WHT
|
||||
float scale = 1.0 / sqrt(float(head_dim));
|
||||
for (uint i = 0; i < head_dim; i++) {
|
||||
rotated[i] *= scale;
|
||||
}
|
||||
|
||||
// Calculate norm
|
||||
float sum_sq = 0.0;
|
||||
for (uint i = 0; i < head_dim; i++) {
|
||||
sum_sq += rotated[i] * rotated[i];
|
||||
}
|
||||
float norm = sqrt(sum_sq);
|
||||
norms[tid] = norm;
|
||||
|
||||
// Quantize and pack
|
||||
float inv_norm = 1.0 / (norm + 1e-9);
|
||||
for (uint i = 0; i < head_dim; i++) {
|
||||
float val = rotated[i] * inv_norm;
|
||||
uint idx = quantize_turbo4_metal(val);
|
||||
|
||||
if (i % 2 == 0) {
|
||||
dst[base_dst + (i / 2)] = (uchar)idx;
|
||||
} else {
|
||||
dst[base_dst + (i / 2)] |= (uchar)(idx << 4);
|
||||
}
|
||||
}
|
||||
// 1. Dequantize K on the fly
|
||||
// 2. Compute dot product with Q
|
||||
// 3. Store score
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
// Lloyd-Max Centroids for N(0, 1/d) where d=128
|
||||
// These are precomputed for 4-bit (16 levels)
|
||||
@@ -15,19 +13,8 @@ static const float turbo4_centroids[16] = {
|
||||
0.1523f, 0.2154f, 0.2800f, 0.3500f // Approximate tail values
|
||||
};
|
||||
|
||||
// Decision boundaries for binary search (precomputed)
|
||||
// boundary[i] = (centroid[i] + centroid[i+1]) / 2
|
||||
static const float turbo4_boundaries[15] = {
|
||||
-0.18385f, -0.1322f, -0.09665f, -0.0683f,
|
||||
-0.04375f, -0.0213f, 0.0f, 0.0213f,
|
||||
0.04375f, 0.0683f, 0.09665f, 0.1322f,
|
||||
0.18385f, 0.2477f, 0.315f
|
||||
};
|
||||
|
||||
// Fast Walsh-Hadamard Transform (In-place)
|
||||
void fwht(float* a, int n) {
|
||||
assert(n > 0 && (n & (n - 1)) == 0 && "n must be power of 2");
|
||||
|
||||
for (int h = 1; h < n; h <<= 1) {
|
||||
for (int i = 0; i < n; i += (h << 1)) {
|
||||
for (int j = i; j < i + h; j++) {
|
||||
@@ -45,70 +32,31 @@ void fwht(float* a, int n) {
|
||||
}
|
||||
}
|
||||
|
||||
// Binary search for Lloyd-Max quantization
|
||||
static inline int quantize_turbo4(float val) {
|
||||
// Binary search through decision boundaries
|
||||
int left = 0, right = 14;
|
||||
while (left < right) {
|
||||
int mid = (left + right) / 2;
|
||||
if (val < turbo4_boundaries[mid]) {
|
||||
right = mid;
|
||||
} else {
|
||||
left = mid + 1;
|
||||
}
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
// PolarQuant Encode (CPU Reference)
|
||||
void polar_quant_encode_turbo4(const float* src, uint8_t* dst, float* norm, int d) {
|
||||
assert(src != nullptr && "src cannot be null");
|
||||
assert(dst != nullptr && "dst cannot be null");
|
||||
assert(norm != nullptr && "norm cannot be null");
|
||||
assert(d > 0 && (d & (d - 1)) == 0 && "d must be power of 2");
|
||||
|
||||
// Use stack allocation for small d (d=128 is 512 bytes)
|
||||
float rotated[128]; // Stack allocation for d=128
|
||||
if (d > 128) {
|
||||
// Fallback to heap for larger d
|
||||
std::vector<float> rotated_vec(src, src + d);
|
||||
fwht(rotated_vec.data(), d);
|
||||
|
||||
// Calculate L2 Norm (Radius)
|
||||
float sum_sq = 0;
|
||||
for (int i = 0; i < d; i++) sum_sq += rotated_vec[i] * rotated_vec[i];
|
||||
*norm = sqrtf(sum_sq);
|
||||
|
||||
// Quantize components
|
||||
float inv_norm = 1.0f / (*norm + 1e-9f);
|
||||
for (int i = 0; i < d; i++) {
|
||||
float val = rotated_vec[i] * inv_norm;
|
||||
int best_idx = quantize_turbo4(val);
|
||||
|
||||
// Pack 4-bit indices
|
||||
if (i % 2 == 0) {
|
||||
dst[i / 2] = (uint8_t)best_idx;
|
||||
} else {
|
||||
dst[i / 2] |= (uint8_t)(best_idx << 4);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Stack-allocated path for d=128
|
||||
memcpy(rotated, src, d * sizeof(float));
|
||||
fwht(rotated, d);
|
||||
|
||||
std::vector<float> rotated(src, src + d);
|
||||
fwht(rotated.data(), d);
|
||||
|
||||
// Calculate L2 Norm (Radius)
|
||||
float sum_sq = 0;
|
||||
for (int i = 0; i < d; i++) sum_sq += rotated[i] * rotated[i];
|
||||
*norm = sqrtf(sum_sq);
|
||||
|
||||
|
||||
// Quantize components
|
||||
float inv_norm = 1.0f / (*norm + 1e-9f);
|
||||
for (int i = 0; i < d; i++) {
|
||||
float val = rotated[i] * inv_norm;
|
||||
int best_idx = quantize_turbo4(val);
|
||||
|
||||
// Simple nearest neighbor search in Lloyd-Max codebook
|
||||
int best_idx = 0;
|
||||
float min_dist = fabsf(val - turbo4_centroids[0]);
|
||||
for (int j = 1; j < 16; j++) {
|
||||
float dist = fabsf(val - turbo4_centroids[j]);
|
||||
if (dist < min_dist) {
|
||||
min_dist = dist;
|
||||
best_idx = j;
|
||||
}
|
||||
}
|
||||
|
||||
// Pack 4-bit indices
|
||||
if (i % 2 == 0) {
|
||||
@@ -121,13 +69,8 @@ void polar_quant_encode_turbo4(const float* src, uint8_t* dst, float* norm, int
|
||||
|
||||
// PolarQuant Decode (CPU Reference)
|
||||
void polar_quant_decode_turbo4(const uint8_t* src, float* dst, float norm, int d) {
|
||||
assert(src != nullptr && "src cannot be null");
|
||||
assert(dst != nullptr && "dst cannot be null");
|
||||
assert(d > 0 && (d & (d - 1)) == 0 && "d must be power of 2");
|
||||
|
||||
for (int i = 0; i < d; i++) {
|
||||
int idx = (i % 2 == 0) ? (src[i / 2] & 0x0F) : (src[i / 2] >> 4);
|
||||
assert(idx >= 0 && idx < 16 && "Invalid index");
|
||||
dst[i] = turbo4_centroids[idx] * norm;
|
||||
}
|
||||
// Inverse WHT is same as Forward WHT for orthogonal matrices
|
||||
|
||||
BIN
tests/__pycache__/test_turboquant.cpython-312-pytest-9.0.2.pyc
Normal file
BIN
tests/__pycache__/test_turboquant.cpython-312-pytest-9.0.2.pyc
Normal file
Binary file not shown.
@@ -1,150 +0,0 @@
|
||||
#include "llama-turbo.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <cassert>
|
||||
|
||||
// Simple test for encode/decode round-trip
|
||||
void test_roundtrip() {
|
||||
const int d = 128;
|
||||
std::vector<float> original(d);
|
||||
std::vector<float> decoded(d);
|
||||
std::vector<uint8_t> packed(d / 2);
|
||||
float norm;
|
||||
|
||||
// Generate random test data
|
||||
for (int i = 0; i < d; i++) {
|
||||
original[i] = (float)rand() / RAND_MAX * 2.0f - 1.0f;
|
||||
}
|
||||
|
||||
// Encode
|
||||
polar_quant_encode_turbo4(original.data(), packed.data(), &norm, d);
|
||||
|
||||
// Decode
|
||||
polar_quant_decode_turbo4(packed.data(), decoded.data(), norm, d);
|
||||
|
||||
// Check round-trip error
|
||||
float max_error = 0.0f;
|
||||
float avg_error = 0.0f;
|
||||
for (int i = 0; i < d; i++) {
|
||||
float error = fabsf(original[i] - decoded[i]);
|
||||
max_error = fmaxf(max_error, error);
|
||||
avg_error += error;
|
||||
}
|
||||
avg_error /= d;
|
||||
|
||||
std::cout << "Round-trip test:" << std::endl;
|
||||
std::cout << " Max error: " << max_error << std::endl;
|
||||
std::cout << " Avg error: " << avg_error << std::endl;
|
||||
std::cout << " Norm: " << norm << std::endl;
|
||||
|
||||
// Check that error is reasonable (should be small due to quantization)
|
||||
assert(max_error < 1.0f && "Round-trip error too large");
|
||||
assert(avg_error < 0.5f && "Average error too large");
|
||||
}
|
||||
|
||||
// Test with known values
|
||||
void test_known_values() {
|
||||
const int d = 128;
|
||||
std::vector<float> zeros(d, 0.0f);
|
||||
std::vector<float> ones(d, 1.0f);
|
||||
std::vector<float> decoded(d);
|
||||
std::vector<uint8_t> packed(d / 2);
|
||||
float norm;
|
||||
|
||||
// Test zeros
|
||||
polar_quant_encode_turbo4(zeros.data(), packed.data(), &norm, d);
|
||||
polar_quant_decode_turbo4(packed.data(), decoded.data(), norm, d);
|
||||
|
||||
std::cout << "Zero test:" << std::endl;
|
||||
std::cout << " Norm: " << norm << std::endl;
|
||||
|
||||
// Test ones
|
||||
polar_quant_encode_turbo4(ones.data(), packed.data(), &norm, d);
|
||||
polar_quant_decode_turbo4(packed.data(), decoded.data(), norm, d);
|
||||
|
||||
std::cout << "Ones test:" << std::endl;
|
||||
std::cout << " Norm: " << norm << std::endl;
|
||||
|
||||
// Check that decoded values are approximately 1.0
|
||||
float avg = 0.0f;
|
||||
for (int i = 0; i < d; i++) {
|
||||
avg += decoded[i];
|
||||
}
|
||||
avg /= d;
|
||||
|
||||
std::cout << " Average decoded value: " << avg << std::endl;
|
||||
assert(fabsf(avg - 1.0f) < 0.5f && "Decoded average should be close to 1.0");
|
||||
}
|
||||
|
||||
// Test edge cases
|
||||
void test_edge_cases() {
|
||||
const int d = 128;
|
||||
std::vector<float> large(d);
|
||||
std::vector<float> small(d);
|
||||
std::vector<float> decoded(d);
|
||||
std::vector<uint8_t> packed(d / 2);
|
||||
float norm;
|
||||
|
||||
// Test large values
|
||||
for (int i = 0; i < d; i++) {
|
||||
large[i] = 1000.0f;
|
||||
}
|
||||
|
||||
polar_quant_encode_turbo4(large.data(), packed.data(), &norm, d);
|
||||
polar_quant_decode_turbo4(packed.data(), decoded.data(), norm, d);
|
||||
|
||||
std::cout << "Large values test:" << std::endl;
|
||||
std::cout << " Norm: " << norm << std::endl;
|
||||
|
||||
// Test small values
|
||||
for (int i = 0; i < d; i++) {
|
||||
small[i] = 0.001f;
|
||||
}
|
||||
|
||||
polar_quant_encode_turbo4(small.data(), packed.data(), &norm, d);
|
||||
polar_quant_decode_turbo4(packed.data(), decoded.data(), norm, d);
|
||||
|
||||
std::cout << "Small values test:" << std::endl;
|
||||
std::cout << " Norm: " << norm << std::endl;
|
||||
}
|
||||
|
||||
// Test error handling
|
||||
void test_error_handling() {
|
||||
const int d = 128;
|
||||
std::vector<float> data(d, 1.0f);
|
||||
std::vector<uint8_t> packed(d / 2);
|
||||
std::vector<float> decoded(d);
|
||||
float norm;
|
||||
|
||||
// Test with null pointers (should assert in debug mode)
|
||||
std::cout << "Error handling tests:" << std::endl;
|
||||
std::cout << " Note: These should trigger assertions in debug mode" << std::endl;
|
||||
|
||||
// Uncomment to test assertions:
|
||||
// polar_quant_encode_turbo4(nullptr, packed.data(), &norm, d);
|
||||
// polar_quant_encode_turbo4(data.data(), nullptr, &norm, d);
|
||||
// polar_quant_encode_turbo4(data.data(), packed.data(), nullptr, d);
|
||||
|
||||
// Test with invalid d (not power of 2)
|
||||
// polar_quant_encode_turbo4(data.data(), packed.data(), &norm, 127);
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "TurboQuant Unit Tests" << std::endl;
|
||||
std::cout << "====================" << std::endl;
|
||||
|
||||
try {
|
||||
test_roundtrip();
|
||||
test_known_values();
|
||||
test_edge_cases();
|
||||
test_error_handling();
|
||||
|
||||
std::cout << std::endl;
|
||||
std::cout << "All tests passed!" << std::endl;
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Test failed: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
141
tests/test_turboquant.py
Normal file
141
tests/test_turboquant.py
Normal file
@@ -0,0 +1,141 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
TurboQuant Test Suite
|
||||
Tests for critical paths in KV cache compression.
|
||||
|
||||
Issue #679: Codebase Genome: turboquant — Full Analysis
|
||||
"""
|
||||
import unittest
|
||||
import subprocess
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
class TestTurboQuant(unittest.TestCase):
|
||||
"""Test TurboQuant implementation."""
|
||||
|
||||
def test_repo_structure(self):
|
||||
"""Verify expected files exist."""
|
||||
required_files = [
|
||||
"llama-turbo.h",
|
||||
"llama-turbo.cpp",
|
||||
"ggml-metal-turbo.metal",
|
||||
"README.md",
|
||||
"GENOME.md"
|
||||
]
|
||||
|
||||
for filename in required_files:
|
||||
filepath = os.path.join(os.path.dirname(__file__), "..", filename)
|
||||
self.assertTrue(os.path.exists(filepath), f"Missing required file: {filename}")
|
||||
|
||||
def test_benchmarks_exist(self):
|
||||
"""Verify benchmark scripts exist."""
|
||||
benchmark_files = [
|
||||
"benchmarks/run_benchmarks.py",
|
||||
"benchmarks/run_perplexity.py",
|
||||
"benchmarks/run_long_session.py"
|
||||
]
|
||||
|
||||
for filename in benchmark_files:
|
||||
filepath = os.path.join(os.path.dirname(__file__), "..", filename)
|
||||
self.assertTrue(os.path.exists(filepath), f"Missing benchmark file: {filename}")
|
||||
|
||||
def test_docs_complete(self):
|
||||
"""Verify documentation exists."""
|
||||
doc_files = [
|
||||
"docs/PROJECT_STATUS.md",
|
||||
"profiles/README.md"
|
||||
]
|
||||
|
||||
for filename in doc_files:
|
||||
filepath = os.path.join(os.path.dirname(__file__), "..", filename)
|
||||
self.assertTrue(os.path.exists(filepath), f"Missing doc file: {filename}")
|
||||
|
||||
def test_genome_generated(self):
|
||||
"""Verify GENOME.md was generated."""
|
||||
genome_path = os.path.join(os.path.dirname(__file__), "..", "GENOME.md")
|
||||
self.assertTrue(os.path.exists(genome_path), "GENOME.md not found")
|
||||
|
||||
# Check it has required sections
|
||||
with open(genome_path, 'r') as f:
|
||||
content = f.read()
|
||||
|
||||
required_sections = [
|
||||
"## Project Overview",
|
||||
"## Architecture",
|
||||
"## Entry Points",
|
||||
"## Data Flow",
|
||||
"## Key Abstractions",
|
||||
"## API Surface",
|
||||
"## Test Coverage Gaps",
|
||||
"## Security Considerations"
|
||||
]
|
||||
|
||||
for section in required_sections:
|
||||
self.assertIn(section, content, f"GENOME.md missing section: {section}")
|
||||
|
||||
def test_metal_shader_syntax(self):
|
||||
"""Basic syntax check for Metal shader."""
|
||||
shader_path = os.path.join(os.path.dirname(__file__), "..", "ggml-metal-turbo.metal")
|
||||
with open(shader_path, 'r') as f:
|
||||
content = f.read()
|
||||
|
||||
# Check for key functions
|
||||
self.assertIn("kernel_fwht_128", content, "Missing kernel_fwht_128 function")
|
||||
self.assertIn("kernel_turbo4_dequant", content, "Missing kernel_turbo4_dequant function")
|
||||
self.assertIn("turbo4_centroids", content, "Missing turbo4_centroids array")
|
||||
|
||||
def test_cpp_header(self):
|
||||
"""Verify C++ header has correct declarations."""
|
||||
header_path = os.path.join(os.path.dirname(__file__), "..", "llama-turbo.h")
|
||||
with open(header_path, 'r') as f:
|
||||
content = f.read()
|
||||
|
||||
# Check for function declarations
|
||||
self.assertIn("polar_quant_encode_turbo4", content, "Missing encode function")
|
||||
self.assertIn("polar_quant_decode_turbo4", content, "Missing decode function")
|
||||
self.assertIn('extern "C"', content, "Missing C linkage")
|
||||
|
||||
class TestBenchmarks(unittest.TestCase):
|
||||
"""Test benchmark infrastructure."""
|
||||
|
||||
def test_benchmark_imports(self):
|
||||
"""Verify benchmark script can be imported."""
|
||||
benchmark_path = os.path.join(os.path.dirname(__file__), "..", "benchmarks", "run_benchmarks.py")
|
||||
|
||||
# Check file exists
|
||||
self.assertTrue(os.path.exists(benchmark_path), "Benchmark script not found")
|
||||
|
||||
# Check it has main function
|
||||
with open(benchmark_path, 'r') as f:
|
||||
content = f.read()
|
||||
|
||||
self.assertIn("def main():", content, "Benchmark script missing main function")
|
||||
self.assertIn("argparse", content, "Benchmark script missing argparse")
|
||||
|
||||
class TestDocumentation(unittest.TestCase):
|
||||
"""Test documentation completeness."""
|
||||
|
||||
def test_readme_sections(self):
|
||||
"""Verify README has required sections."""
|
||||
readme_path = os.path.join(os.path.dirname(__file__), "..", "README.md")
|
||||
with open(readme_path, 'r') as f:
|
||||
content = f.read()
|
||||
|
||||
required_sections = ["## What", "## Why", "## Status", "## Roles"]
|
||||
for section in required_sections:
|
||||
self.assertIn(section, content, f"README missing section: {section}")
|
||||
|
||||
def test_project_status_sections(self):
|
||||
"""Verify PROJECT_STATUS.md has required sections."""
|
||||
status_path = os.path.join(os.path.dirname(__file__), "..", "docs", "PROJECT_STATUS.md")
|
||||
with open(status_path, 'r') as f:
|
||||
content = f.read()
|
||||
|
||||
# Check for key findings
|
||||
self.assertIn("73%", content, "Missing 73% savings metric")
|
||||
self.assertIn("PolarQuant", content, "Missing PolarQuant references")
|
||||
self.assertIn("Metal", content, "Missing Metal shader references")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user