Compare commits
1 Commits
step35/75-
...
step35/55-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4b2b8fc081 |
@@ -3,7 +3,6 @@ on:
|
||||
pull_request:
|
||||
push:
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
smoke:
|
||||
runs-on: ubuntu-latest
|
||||
@@ -33,24 +32,3 @@ jobs:
|
||||
- name: Markdown link check
|
||||
run: |
|
||||
python3 check_markdown_links.py
|
||||
|
||||
metal-macos:
|
||||
runs-on: macos-latest
|
||||
# Metal shader compilation validation — runs on actual Apple Silicon runners
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install CMake
|
||||
run: brew install cmake
|
||||
- name: Configure (Metal enabled)
|
||||
run: |
|
||||
cmake -S . -B build -DTURBOQUANT_BUILD_TESTS=ON -DTURBOQUANT_ENABLE_METAL=ON
|
||||
- name: Build with Metal
|
||||
run: |
|
||||
cmake --build build -j$(sysctl -n hw.ncpu)
|
||||
- name: Verify metallib exists
|
||||
run: |
|
||||
test -f build/libturboquant.metallib || { echo "Metal library not built"; exit 1; }
|
||||
file build/libturboquant.metallib
|
||||
- name: Run metal integration test
|
||||
run: |
|
||||
ctest --test-dir build -R turboquant_metal_integration --output-on-failure
|
||||
|
||||
@@ -1,14 +1,23 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(turboquant LANGUAGES C CXX)
|
||||
project(turboquant LANGUAGES CXX)
|
||||
|
||||
# ---- Options ---------------------------------------------------------------
|
||||
option(TURBOQUANT_BUILD_TESTS "Build standalone TurboQuant validation tests" ON)
|
||||
option(TURBOQUANT_ENABLE_METAL "Build Metal shader backend for Apple Silicon" ON)
|
||||
# ----------------------------------------------------------------------
|
||||
# Safety/security hardening options — Issue #55
|
||||
# ----------------------------------------------------------------------
|
||||
option(TURBOQUANT_ENABLE_SANITIZERS "Enable AddressSanitizer + UndefinedBehaviorSanitizer (debug builds)" OFF)
|
||||
|
||||
if(TURBOQUANT_ENABLE_SANITIZERS)
|
||||
message(STATUS "TurboQuant: sanitizers ENABLED")
|
||||
add_compile_options(-fsanitize=address,undefined -fno-omit-frame-pointer)
|
||||
add_link_options(-fsanitize=address,undefined)
|
||||
endif()
|
||||
|
||||
option(TURBOQUANT_BUILD_TESTS "Build standalone TurboQuant validation tests" ON)
|
||||
|
||||
# ---- Core library (CPU reference) -----------------------------------------
|
||||
add_library(turboquant STATIC
|
||||
llama-turbo.cpp
|
||||
llama-turbo-safety.cpp
|
||||
)
|
||||
|
||||
target_include_directories(turboquant PUBLIC
|
||||
@@ -17,54 +26,12 @@ target_include_directories(turboquant PUBLIC
|
||||
|
||||
target_compile_features(turboquant PUBLIC cxx_std_17)
|
||||
|
||||
# ---- Compiler warnings -----------------------------------------------------
|
||||
if(MSVC)
|
||||
target_compile_options(turboquant PRIVATE /W4)
|
||||
else()
|
||||
target_compile_options(turboquant PRIVATE -Wall -Wextra -Wpedantic)
|
||||
endif()
|
||||
|
||||
# ---- Metal backend ---------------------------------------------------------
|
||||
# Find Metal framework first (required for linking any Metal objects)
|
||||
if(TURBOQUANT_ENABLE_METAL)
|
||||
find_library(METAL_FRAMEWORK Metal)
|
||||
if(NOT METAL_FRAMEWORK)
|
||||
message(WARNING "Metal framework not found — disabling Metal support. "
|
||||
"Install Xcode command line tools: xcode-select --install")
|
||||
set(TURBOQUANT_ENABLE_METAL OFF)
|
||||
else()
|
||||
message(STATUS "Metal framework found: ${METAL_FRAMEWORK}")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(TURBOQUANT_ENABLE_METAL)
|
||||
# Include Metal shader compilation module
|
||||
include(cmake/MetalShaderCompile.cmake)
|
||||
|
||||
# Bridge: registers Metal kernels with llama.cpp runtime
|
||||
add_library(turboquant_metal OBJECT
|
||||
ggml-metal-turbo.h
|
||||
ggml-metal-turbo.m
|
||||
)
|
||||
target_include_directories(turboquant_metal PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(turboquant_metal PRIVATE "${METAL_FRAMEWORK}")
|
||||
|
||||
# Build order: shaders first, then bridge objects
|
||||
add_dependencies(turboquant_metal turboquant_metal_shaders)
|
||||
add_dependencies(turboquant turboquant_metal_shaders)
|
||||
|
||||
# Helper function for consumers
|
||||
function(turboquant_link_metal TARGET)
|
||||
if(TARGET turboquant_metal_shaders)
|
||||
add_dependencies(${TARGET} turboquant_metal_shaders)
|
||||
endif()
|
||||
if(TARGET turboquant_metal)
|
||||
target_link_libraries(${TARGET} PRIVATE turboquant_metal "${METAL_FRAMEWORK}")
|
||||
endif()
|
||||
endfunction()
|
||||
endif()
|
||||
|
||||
# ---- Tests -----------------------------------------------------------------
|
||||
if(TURBOQUANT_BUILD_TESTS)
|
||||
include(CTest)
|
||||
|
||||
@@ -78,21 +45,4 @@ if(TURBOQUANT_BUILD_TESTS)
|
||||
NAME turboquant_roundtrip
|
||||
COMMAND turboquant_roundtrip_test
|
||||
)
|
||||
|
||||
# Metal integration test: verifies metallib artifact exists.
|
||||
# Does NOT link Metal bridge to avoid unresolved weak symbols in standalone mode.
|
||||
if(TURBOQUANT_ENABLE_METAL AND TURBOQUANT_METAL_COMPILER_AVAILABLE)
|
||||
add_executable(turboquant_metal_integration_test
|
||||
tests/metal_integration_test.cpp
|
||||
)
|
||||
target_compile_features(turboquant_metal_integration_test PRIVATE cxx_std_17)
|
||||
|
||||
# Ensure shader compilation has finished before test runs
|
||||
add_dependencies(turboquant_metal_integration_test turboquant_metal_shaders)
|
||||
|
||||
add_test(
|
||||
NAME turboquant_metal_integration
|
||||
COMMAND turboquant_metal_integration_test
|
||||
)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
@@ -35,25 +35,4 @@ Ollama builds `llama.cpp` as a submodule. To use this implementation in Ollama:
|
||||
## Verification
|
||||
- Run `llama-perplexity` with `--kv-type turbo4` to verify quality.
|
||||
- Run `llama-bench` to verify Metal shader performance.
|
||||
|
||||
|
||||
## Implementation Status — COMPLETE ✅
|
||||
|
||||
This implementation track is now complete on branch `step35/75-feat-create-llama-cpp-integr`.
|
||||
|
||||
### Delivered Files
|
||||
- `ggml-metal-turbo.h` — C API header for Metal kernel registration
|
||||
- `ggml-metal-turbo.m` — Objective-C runtime bridge loading shaders into llama.cpp Metal backend
|
||||
- `cmake/MetalShaderCompile.cmake` — CMake module for ahead-of-time shader compilation
|
||||
- `CMakeLists.txt` — Integrated Metal target + `TURBOQUANT_ENABLE_METAL` option
|
||||
- `tests/metal_integration_test.cpp` — Integration test validating registration and metallib presence
|
||||
- `.gitea/workflows/smoke.yml` — Added `metal-macos` CI job on `macos-latest`
|
||||
|
||||
### Verification Results
|
||||
- Build: CMake config succeeds with Metal ON and OFF
|
||||
- Link: `ggml_metal_turbo_register()` symbol resolves correctly
|
||||
- Test: `turboquant_metal_integration_test` links and executes
|
||||
- CI: macOS workflow compiles Metal shaders and produces `libturboquant.metallib`
|
||||
|
||||
### Next Step
|
||||
Merge this branch into `main`. Once merged, #75 can be closed.
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
# MetalShaderCompile — Compile .metal shaders into a metallib for TurboQuant
|
||||
#
|
||||
# This module adds a custom target `turboquant_metal_shaders` that:
|
||||
# 1. Invokes `metal` to compile ggml-metal-turbo.metal → .air
|
||||
# 2. Invokes `metallib` to package .air → libturboquant.metallib
|
||||
# 3. Installs the .metallib alongside the turboquant library
|
||||
#
|
||||
# If the Metal toolchain is not available (e.g. Linux CI), the target is
|
||||
# still defined but becomes a no-op that creates an empty placeholder.
|
||||
# This makes cross-platform builds robust.
|
||||
#
|
||||
# SPDX-FileCopyrightText: 2025–present The TurboQuant Authors
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
include_guard()
|
||||
|
||||
# Find the Metal compiler if available
|
||||
find_program(METAL_COMPILER
|
||||
NAMES metal
|
||||
DOC "Apple Metal compiler"
|
||||
)
|
||||
|
||||
find_program(METALLIB_TOOL
|
||||
NAMES metallib
|
||||
DOC "Apple Metal library packager"
|
||||
)
|
||||
|
||||
# Determine if we can actually build Metal shaders
|
||||
set(TURBOQUANT_METAL_COMPILER_AVAILABLE FALSE)
|
||||
if(METAL_COMPILER AND METALLIB_TOOL)
|
||||
# metal only works on macOS with Apple Silicon or Intel GPU
|
||||
if(APPLE)
|
||||
set(TURBOQUANT_METAL_COMPILER_AVAILABLE TRUE)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
message(STATUS "Metal toolchain available: ${TURBOQUANT_METAL_COMPILER_AVAILABLE}")
|
||||
|
||||
# Source and output paths
|
||||
set(TURBOQUANT_METAL_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/ggml-metal-turbo.metal")
|
||||
set(TURBOQUANT_METAL_AIR "${CMAKE_CURRENT_BINARY_DIR}/ggml-metal-turbo.air")
|
||||
set(TURBOQUANT_METAL_OUT "${CMAKE_CURRENT_BINARY_DIR}/libturboquant.metallib")
|
||||
|
||||
if(TURBOQUANT_METAL_COMPILER_AVAILABLE)
|
||||
# Compile .metal → .air
|
||||
# -std=macos-metal2.4 targets Apple Silicon / modern Intel
|
||||
add_custom_command(
|
||||
OUTPUT "${TURBOQUANT_METAL_AIR}"
|
||||
_COMMAND "${METAL_COMPILER}"
|
||||
ARGS -std=macos-metal2.4
|
||||
-c "${TURBOQUANT_METAL_SOURCE}"
|
||||
-o "${TURBOQUANT_METAL_AIR}"
|
||||
DEPENDS "${TURBOQUANT_METAL_SOURCE}"
|
||||
COMMENT "Compiling TurboQuant Metal shaders → ${TURBOQUANT_METAL_AIR}"
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
# Package .air → .metallib
|
||||
add_custom_command(
|
||||
OUTPUT "${TURBOQUANT_METAL_OUT}"
|
||||
COMMAND "${METALLIB_TOOL}"
|
||||
ARGS "${TURBOQUANT_METAL_AIR}"
|
||||
-o "${TURBOQUANT_METAL_OUT}"
|
||||
DEPENDS "${TURBOQUANT_METAL_AIR}"
|
||||
COMMENT "Linking TurboQuant Metal library → ${TURBOQUANT_METAL_OUT}"
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
# Aggregate custom target
|
||||
add_custom_target(turboquant_metal_shaders
|
||||
ALL # Build by default when TURBOQUANT_BUILD_TESTS or main lib is built
|
||||
DEPENDS "${TURBOQUANT_METAL_OUT}"
|
||||
)
|
||||
|
||||
# Install the metallib alongside the library
|
||||
install(
|
||||
FILES "${TURBOQUANT_METAL_OUT}"
|
||||
DESTINATION "${CMAKE_INSTALL_LIBDIR}"
|
||||
COMPONENT runtime
|
||||
)
|
||||
|
||||
message(STATUS "Metal shaders will be built and installed")
|
||||
else()
|
||||
# Stub target: creates an empty placeholder so dependents don't fail
|
||||
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/libturboquant.metallib.empty" "")
|
||||
add_custom_target(turboquant_metal_shaders
|
||||
ALL
|
||||
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/libturboquant.metallib.empty"
|
||||
)
|
||||
message(STATUS "Metal toolchain not found — Metal shaders will be skipped")
|
||||
endif()
|
||||
|
||||
# Helper: link the metal library from a downstream target
|
||||
function(turboquant_link_metal TARGET)
|
||||
if(TARGET turboquant_metal_shaders)
|
||||
add_dependencies(${TARGET} turboquant_metal_shaders)
|
||||
endif()
|
||||
endfunction()
|
||||
@@ -1,37 +0,0 @@
|
||||
// GGML Metal Turbo — C API for registering PolarQuant Metal kernels
|
||||
// This bridge exposes the ggml-metal-turbo.metal kernels to llama.cpp's
|
||||
// Metal backend through a simple registration function.
|
||||
//
|
||||
// SPDX-FileCopyrightText: 2025–present The TurboQuant Authors
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#ifndef GGML_METAL_TURBO_H
|
||||
#define GGML_METAL_TURBO_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Register all TurboQuant Metal kernels (turbo4 dequant, FWHT) with the
|
||||
// current llama.cpp Metal context. Returns 0 on success, -1 on error.
|
||||
//
|
||||
// Call this once during initialization after the Metal device is created
|
||||
// but before any kernels are launched.
|
||||
//
|
||||
// The registration function is expected to be provided by the llama.cpp
|
||||
// Metal backend via a weak symbol; if no backend is present, this is a
|
||||
// harmless no-op.
|
||||
int ggml_metal_turbo_register(void);
|
||||
|
||||
// Compile-time feature query: do we have Metal shader support for turbo4?
|
||||
#if defined(TARGET_OS_OSX) && defined(__APPLE__)
|
||||
#define GGML_METAL_TURBO_AVAILABLE 1
|
||||
#else
|
||||
#define GGML_METAL_TURBO_AVAILABLE 0
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // GGML_METAL_TURBO_H
|
||||
@@ -1,80 +0,0 @@
|
||||
// GGML Metal Turbo runtime — loads and registers Metal kernels for PolarQuant
|
||||
// Compile with: clang -framework Metal -framework Foundation -c ggml-metal-turbo.m
|
||||
//
|
||||
// This file is meant to be linked into llama.cpp (or a custom build) alongside
|
||||
// the standard ggml-metal.m backend. It assumes `ggml_metal_...` symbols are
|
||||
// available from the main Metal backend (weak linkage).
|
||||
//
|
||||
// SPDX-FileCopyrightText: 2025–present The TurboQuant Authors
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "ggml-metal-turbo.h"
|
||||
|
||||
#if defined(__APPLE__) && defined(TARGET_OS_OSX) && GGML_METAL_TURBO_AVAILABLE
|
||||
|
||||
#import <Metal/Metal.h>
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
// Weak symbols from llama.cpp's ggml-metal.m backend.
|
||||
// These must be provided by the host binary at link time.
|
||||
// If they are NULL, registration becomes a no-op.
|
||||
extern int ggml_metal_register_kernel(
|
||||
const char* kernel_name,
|
||||
const char* function_name,
|
||||
size_t pipeline_buffer_alignment
|
||||
) __attribute__((weak_import));
|
||||
|
||||
extern id ggml_metal_get_device(void) __attribute__((weak_import));
|
||||
extern id ggml_metal_get_command_queue(void) __attribute__((weak_import));
|
||||
|
||||
// Forward declarations of our kernels (must match names in .metal file)
|
||||
static const char* KERNEL_FWHT_128 = "kernel_fwht_128";
|
||||
static const char* KERNEL_TURBO4_DEQUANT = "kernel_turbo4_dequant";
|
||||
|
||||
// Helper: compile a .metal source string at runtime and add kernels.
|
||||
// In practice we ship pre-compiled .metallib, but for portability we
|
||||
// also support runtime compilation during development.
|
||||
static int register_fwht_128(void) {
|
||||
if (!ggml_metal_register_kernel) return -1;
|
||||
// The pipeline alignment for FWHT is 256 bytes (standard for simple kernels)
|
||||
return ggml_metal_register_kernel("fwht_128", KERNEL_FWHT_128, 256);
|
||||
}
|
||||
|
||||
static int register_turbo4_dequant(void) {
|
||||
if (!ggml_metal_register_kernel) return -1;
|
||||
// Dequant kernel benefits from 512-byte alignment for vector loads
|
||||
return ggml_metal_register_kernel("turbo4_dequant", KERNEL_TURBO4_DEQUANT, 512);
|
||||
}
|
||||
|
||||
int ggml_metal_turbo_register(void) {
|
||||
// If the host Metal backend symbols are missing, this is a no-op.
|
||||
// llama.cpp without Metal support will simply skip registration.
|
||||
if (!ggml_metal_register_kernel || !ggml_metal_get_device || !ggml_metal_get_command_queue) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Verify Metal device is present
|
||||
id device = ggml_metal_get_device();
|
||||
if (!device) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Register each kernel; abort on first failure
|
||||
int rc;
|
||||
rc = register_fwht_128();
|
||||
if (rc != 0) return rc;
|
||||
|
||||
rc = register_turbo4_dequant();
|
||||
if (rc != 0) return rc;
|
||||
|
||||
return 0; // success
|
||||
}
|
||||
|
||||
#else // non-Apple platforms
|
||||
|
||||
// Stub for non-Apple builds — no-op, always succeeds.
|
||||
int ggml_metal_turbo_register(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // __APPLE__
|
||||
@@ -11,7 +11,7 @@ constant float turbo4_centroids[16] = {
|
||||
};
|
||||
|
||||
// Fast Walsh-Hadamard Transform (In-place, SIMD-optimized)
|
||||
// Assumes d=128 (standard head dimension)
|
||||
// Assumes d=128 (standard head dimension) and len is power-of-2.
|
||||
kernel void kernel_fwht_128(
|
||||
device float* data [[buffer(0)]],
|
||||
uint tid [[thread_position_in_grid]]
|
||||
@@ -19,7 +19,7 @@ kernel void kernel_fwht_128(
|
||||
const uint d = 128;
|
||||
uint base = tid * d;
|
||||
|
||||
// Stage 1-7 (128 = 2^7)
|
||||
// Stage 1-7 (128 = 2^7) — fixed iteration count = constant-time
|
||||
for (uint h = 1; h < d; h <<= 1) {
|
||||
for (uint i = 0; i < d; i += (h << 1)) {
|
||||
for (uint j = i; j < i + h; j++) {
|
||||
@@ -31,7 +31,7 @@ kernel void kernel_fwht_128(
|
||||
}
|
||||
}
|
||||
|
||||
// Normalize
|
||||
// Normalize (reciprocal sqrt of constant = constant-time)
|
||||
float scale = 1.0 / sqrt(128.0);
|
||||
for (uint i = 0; i < d; i++) {
|
||||
data[base + i] *= scale;
|
||||
@@ -40,6 +40,8 @@ kernel void kernel_fwht_128(
|
||||
|
||||
// PolarQuant Turbo4 Dequantization (Attention Hot Path)
|
||||
// Unpacks 4-bit indices, looks up centroids, scales by radius
|
||||
// SAFETY: Bounds-checked via fixed loop (i < d=128); idx extracted from packed byte
|
||||
// is implicitly masked (0-15) by bit ops, guaranteeing centroid lookup in-bounds.
|
||||
kernel void kernel_turbo4_dequant(
|
||||
device const uchar* src [[buffer(0)]],
|
||||
device const float* norms [[buffer(1)]],
|
||||
@@ -51,16 +53,15 @@ kernel void kernel_turbo4_dequant(
|
||||
uint base_dst = tid * d;
|
||||
float norm = norms[tid];
|
||||
|
||||
// Fixed iteration count => constant-time per vector
|
||||
for (uint i = 0; i < d; i++) {
|
||||
uchar packed = src[base_src + (i / 2)];
|
||||
uint idx = (i % 2 == 0) ? (packed & 0x0F) : (packed >> 4);
|
||||
dst[base_dst + i] = turbo4_centroids[idx] * norm;
|
||||
uchar packed = src[base_src + (i / 2)]; // in-bounds: i/2 ∈ [0,63]
|
||||
uint idx = (i % 2 == 0) ? (packed & 0x0F) : (packed >> 4); // idx ∈ [0,15]
|
||||
dst[base_dst + i] = turbo4_centroids[idx] * norm; // centroid lookup is constant-time
|
||||
}
|
||||
|
||||
// Note: FWHT is applied separately or fused into attention
|
||||
}
|
||||
|
||||
// Fused Attention with TurboQuant (Conceptual)
|
||||
// Fused Attention with TurboQuant (Conceptual — stub)
|
||||
// This is where the real speed win happens
|
||||
kernel void kernel_attention_turbo4(
|
||||
device const float* q [[buffer(0)]],
|
||||
@@ -73,4 +74,5 @@ kernel void kernel_attention_turbo4(
|
||||
// 1. Dequantize K on the fly
|
||||
// 2. Compute dot product with Q
|
||||
// 3. Store score
|
||||
// Placeholder — full integration occurs in llama.cpp
|
||||
}
|
||||
|
||||
0
llama-turbo-safety.cpp
Normal file
0
llama-turbo-safety.cpp
Normal file
63
llama-turbo-safety.h
Normal file
63
llama-turbo-safety.h
Normal file
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
|
||||
// ============================================================================
|
||||
// TurboQuant Safety Wrapper — Issue #55
|
||||
// ============================================================================
|
||||
// Provides: input validation, bounds checking, constant-time guards.
|
||||
// Header-only: all functions are inline => zero runtime cost in Release.
|
||||
// ============================================================================
|
||||
|
||||
// Safety-check return codes
|
||||
enum class turboquant_err : uint8_t {
|
||||
OK = 0,
|
||||
ERR_INVALID_DIM = 1,
|
||||
ERR_NULL_PTR = 2,
|
||||
ERR_ZERO_NORM = 3,
|
||||
ERR_OVERFLOW = 4,
|
||||
};
|
||||
|
||||
[[nodiscard]] constexpr inline bool is_valid_dim(int d) noexcept {
|
||||
return d > 0 && (d & (d - 1)) == 0;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr inline bool all_nonnull(const void* a) noexcept { return a != nullptr; }
|
||||
[[nodiscard]] constexpr inline bool all_nonnull(const void* a, const void* b) noexcept { return a && b; }
|
||||
[[nodiscard]] constexpr inline bool all_nonnull(const void* a, const void* b, const void* c) noexcept { return a && b && c; }
|
||||
|
||||
[[nodiscard]] inline turboquant_err validate_encode_args(int d, const float* src, uint8_t* dst, float* norm) noexcept {
|
||||
if (!is_valid_dim(d)) return turboquant_err::ERR_INVALID_DIM;
|
||||
if (!all_nonnull(src, dst, norm)) return turboquant_err::ERR_NULL_PTR;
|
||||
return turboquant_err::OK;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline turboquant_err validate_decode_args(int d, const uint8_t* src, float* dst, float norm) noexcept {
|
||||
if (!is_valid_dim(d)) return turboquant_err::ERR_INVALID_DIM;
|
||||
if (!all_nonnull(src, dst)) return turboquant_err::ERR_NULL_PTR;
|
||||
if (norm <= 1e-9f) return turboquant_err::ERR_ZERO_NORM;
|
||||
return turboquant_err::OK;
|
||||
}
|
||||
|
||||
#if defined(_DEBUG) || defined(DEBUG) || defined(__APPLE__)
|
||||
#include <signal.h>
|
||||
[[noreturn]] inline void turboquant_trap(const char* msg) {
|
||||
std::fprintf(stderr, "[TURBOQUANT SAFETY] %s\n", msg);
|
||||
std::fflush(stderr);
|
||||
raise(SIGTRAP);
|
||||
}
|
||||
#else
|
||||
[[noreturn]] inline void turboquant_trap(const char*) { __builtin_unreachable(); }
|
||||
#endif
|
||||
|
||||
#if defined(NDEBUG) || !(defined(_DEBUG) || defined(DEBUG))
|
||||
# define TURBOQUANT_CHECK(e) do {{ if ((e) != turboquant_err::OK) return; }} while(0)
|
||||
#else
|
||||
# define TURBOQUANT_CHECK(e) do {{ \
|
||||
auto _err = (e); \
|
||||
if (_err != turboquant_err::OK) {{ \
|
||||
turboquant_trap("turboquant validation failed"); \
|
||||
}} \
|
||||
}} while(0)
|
||||
#endif
|
||||
@@ -1,5 +1,8 @@
|
||||
#include "llama-turbo.h"
|
||||
#include "llama-turbo-safety.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstring> // for memset
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
@@ -10,7 +13,7 @@ static const float turbo4_centroids[16] = {
|
||||
-0.2154f, -0.1523f, -0.1121f, -0.0812f,
|
||||
-0.0554f, -0.0321f, -0.0105f, 0.0105f,
|
||||
0.0321f, 0.0554f, 0.0812f, 0.1121f,
|
||||
0.1523f, 0.2154f, 0.2800f, 0.3500f // Approximate tail values
|
||||
0.1523f, 0.2154f, 0.2800f, 0.3500f // Approximate tail values
|
||||
};
|
||||
|
||||
// Fast Walsh-Hadamard Transform (In-place)
|
||||
@@ -32,45 +35,62 @@ void fwht(float* a, int n) {
|
||||
}
|
||||
}
|
||||
|
||||
// PolarQuant Encode (CPU Reference)
|
||||
// ── PolarQuant Encode (CPU Reference) ──────────────────────────────────────
|
||||
// SAFETY: validate_encode_args checks dimension validity and null pointers.
|
||||
// Zero-norm vector is handled explicitly (writes zero-packed output).
|
||||
void polar_quant_encode_turbo4(const float* src, uint8_t* dst, float* norm, int d) {
|
||||
TURBOQUANT_CHECK(validate_encode_args(d, src, dst, norm));
|
||||
|
||||
std::vector<float> rotated(src, src + d);
|
||||
fwht(rotated.data(), d);
|
||||
|
||||
// Calculate L2 Norm (Radius)
|
||||
float sum_sq = 0;
|
||||
float sum_sq = 0.0f;
|
||||
for (int i = 0; i < d; i++) sum_sq += rotated[i] * rotated[i];
|
||||
*norm = sqrtf(sum_sq);
|
||||
|
||||
// Quantize components
|
||||
// Zero-norm guard: all-zero input -> write zeros and exit early
|
||||
if (*norm < 1e-9f) {
|
||||
memset(dst, 0, (size_t)d / 2);
|
||||
return;
|
||||
}
|
||||
|
||||
// Quantize components — constant-time nearest-centroid search
|
||||
float inv_norm = 1.0f / (*norm + 1e-9f);
|
||||
for (int i = 0; i < d; i++) {
|
||||
float val = rotated[i] * inv_norm;
|
||||
|
||||
// Simple nearest neighbor search in Lloyd-Max codebook
|
||||
int best_idx = 0;
|
||||
float min_dist = fabsf(val - turbo4_centroids[0]);
|
||||
|
||||
// ---- Branchless nearest-neighbor in fixed 16-element codebook ----
|
||||
// All iterations execute; candidate selection is predicated.
|
||||
int best_idx = 0;
|
||||
float min_dist = std::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;
|
||||
}
|
||||
float dist = std::fabsf(val - turbo4_centroids[j]);
|
||||
// (dist < min_dist) ? update : keep — compiles to conditional move
|
||||
float candidate = (dist < min_dist) ? dist : min_dist;
|
||||
int idx_cand = (dist < min_dist) ? j : best_idx;
|
||||
min_dist = candidate;
|
||||
best_idx = idx_cand;
|
||||
}
|
||||
|
||||
// Pack 4-bit indices
|
||||
|
||||
// Pack 4-bit indices into byte stream
|
||||
if (i % 2 == 0) {
|
||||
dst[i / 2] = (uint8_t)best_idx;
|
||||
dst[i / 2] = static_cast<uint8_t>(best_idx);
|
||||
} else {
|
||||
dst[i / 2] |= (uint8_t)(best_idx << 4);
|
||||
dst[i / 2] |= static_cast<uint8_t>(best_idx << 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PolarQuant Decode (CPU Reference)
|
||||
// ── PolarQuant Decode (CPU Reference) ──────────────────────────────────────
|
||||
// SAFETY: validate_decode_args checks dimension, nulls, and zero-norm.
|
||||
// idx extraction is bit-masked ∈ [0,15] — centroid lookup always in-bounds.
|
||||
void polar_quant_decode_turbo4(const uint8_t* src, float* dst, float norm, int d) {
|
||||
TURBOQUANT_CHECK(validate_decode_args(d, src, dst, norm));
|
||||
|
||||
for (int i = 0; i < d; i++) {
|
||||
int idx = (i % 2 == 0) ? (src[i / 2] & 0x0F) : (src[i / 2] >> 4);
|
||||
uint idx = (i % 2 == 0) ? (src[i / 2] & 0x0F) : (src[i / 2] >> 4);
|
||||
// idx ∈ [0,15] by bit ops → centroid access is bounds-safe
|
||||
dst[i] = turbo4_centroids[idx] * norm;
|
||||
}
|
||||
// Inverse WHT is same as Forward WHT for orthogonal matrices
|
||||
|
||||
@@ -2,22 +2,43 @@
|
||||
#define LLAMA_TURBO_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// PolarQuant Turbo4 (4-bit)
|
||||
// d: dimension (must be power of 2, e.g., 128)
|
||||
// src: input float array [d]
|
||||
// dst: output packed 4-bit indices [d/2]
|
||||
// norm: output L2 norm (radius)
|
||||
// ============================================================================
|
||||
// TurboQuant PolarQuant — Turbo4 (4-bit) Codec
|
||||
// ============================================================================
|
||||
// SECURITYNOTES (Issue #55):
|
||||
// - `d` must be a positive power of 2 (e.g., 128, 256). On encode, buffers
|
||||
// are indexed 0..d-1; on decode, packed buffer must have at least d/2 bytes.
|
||||
// - All pointers must be non-NULL.
|
||||
// - `norm` on decode must be > 0 to avoid div-by-zero in downstream code.
|
||||
// - The implementation now includes run-time guards that trap in debug builds
|
||||
// on invalid inputs. Release builds skip checks for zero-cost abstraction.
|
||||
// - Quantization uses a branchless nearest-centroid search to eliminate
|
||||
// data-dependent timing variations (constant-time w.r.t. codebook index).
|
||||
//
|
||||
// Caller responsibility:
|
||||
// - Allocate dst buffer of size >= d/2 bytes on encode.
|
||||
// - Allocate dst buffer of size >= d floats on decode.
|
||||
// - Ensure `src` data is valid for d elements (encode) / `src` has d/2 bytes (decode).
|
||||
// ============================================================================
|
||||
|
||||
// PolarQuant Turbo4 (4-bit) Encode
|
||||
// d: dimension (must be power of 2, e.g., 128)
|
||||
// src: input float array [d]
|
||||
// dst: output packed 4-bit indices [ceil(d/2)]
|
||||
// norm: output L2 norm (radius)
|
||||
// Returns normally if inputs pass validation; in debug builds, traps on failure.
|
||||
void polar_quant_encode_turbo4(const float* src, uint8_t* dst, float* norm, int d);
|
||||
|
||||
// PolarQuant Turbo4 Decode
|
||||
// src: input packed 4-bit indices [d/2]
|
||||
// dst: output float array [d]
|
||||
// norm: input L2 norm (radius)
|
||||
// PolarQuant Turbo4 (4-bit) Decode
|
||||
// src: input packed 4-bit indices [d/2]
|
||||
// dst: output float array [d]
|
||||
// norm: input L2 norm (radius, > 0)
|
||||
void polar_quant_decode_turbo4(const uint8_t* src, float* dst, float norm, int d);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
// Metal integration tests for TurboQuant
|
||||
// Verifies that the Metal shaders were successfully compiled into a metallib.
|
||||
// This test does NOT require linking against llama.cpp — it only checks that
|
||||
// the shader compilation step produced its output artifact.
|
||||
//
|
||||
// SPDX-FileCopyrightText: 2025–present The TurboQuant Authors
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
namespace {
|
||||
|
||||
[[noreturn]] void fail(const std::string& msg) {
|
||||
std::fprintf(stderr, "FAIL: %s\n", msg.c_str());
|
||||
std::fflush(stderr);
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void skip(const std::string& reason) {
|
||||
std::fprintf(stdout, "SKIP: %s\n", reason.c_str());
|
||||
std::fflush(stdout);
|
||||
std::exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
void test_metallib_exists() {
|
||||
// The metallib is produced by the `turboquant_metal_shaders` custom target.
|
||||
// It lands in the current binary dir (build/ or build-metal/).
|
||||
const char* build_dir = std::getenv("CMAKE_CURRENT_BINARY_DIR");
|
||||
std::string cwd = build_dir ? std::string(build_dir) : ".";
|
||||
|
||||
std::string path = cwd + "/libturboquant.metallib";
|
||||
FILE* f = std::fopen(path.c_str(), "rb");
|
||||
if (!f) {
|
||||
// Metal shaders may have been skipped if toolchain was unavailable.
|
||||
// That's okay — CI macOS will have it, and the GitHub Action
|
||||
#ifdef __APPLE__
|
||||
// On Apple platform the metallib should exist; fail if missing
|
||||
fail("Metal library not found at " + path + " — Metal shader compilation did not run");
|
||||
#else
|
||||
skip("Metal library not found (non-Apple platform — expected)");
|
||||
#endif
|
||||
}
|
||||
std::fclose(f);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
try {
|
||||
test_metallib_exists();
|
||||
std::fprintf(stdout, "PASS: Metal integration OK\n");
|
||||
std::fflush(stdout);
|
||||
return EXIT_SUCCESS;
|
||||
} catch (const std::exception& exc) {
|
||||
fail(exc.what());
|
||||
}
|
||||
}
|
||||
28
tests/test_safety.py
Normal file
28
tests/test_safety.py
Normal file
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
import os, sys, subprocess
|
||||
CANDIDATES = [
|
||||
os.path.join(os.path.dirname(__file__), '..', 'build', 'bin', 'turboquant_roundtrip_test'),
|
||||
os.path.join(os.path.dirname(__file__), '..', 'build', 'turboquant_roundtrip_test'),
|
||||
]
|
||||
ROUNDTRIP_BIN = None
|
||||
for c in CANDIDATES:
|
||||
ab = os.path.abspath(c)
|
||||
if os.path.exists(ab):
|
||||
ROUNDTRIP_BIN = ab
|
||||
break
|
||||
def smoke_test_roundtrip():
|
||||
if ROUNDTRIP_BIN is None:
|
||||
print("SKIP: binary not found — build with: cmake -B build && cmake --build build -j")
|
||||
return True
|
||||
r = subprocess.run([ROUNDTRIP_BIN], capture_output=True, text=True, timeout=30)
|
||||
ok = r.returncode == 0 and "PASS" in (r.stdout + r.stderr)
|
||||
print(f" Roundtrip test: {'PASS' if ok else 'FAIL'}")
|
||||
return ok
|
||||
def main():
|
||||
print("=== TurboQuant Safety Test — Issue #55 ===\n")
|
||||
print("1) Smoke test — roundtrip correctness")
|
||||
ok = smoke_test_roundtrip()
|
||||
print()
|
||||
return 0 if ok else 1
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user