bsh-harness / src /main.c
SNAPKITTYWEST's picture
October 2026 main drop: mirror from GitHub
fb6457c verified
Raw History Blame Contribute Delete
10.9 kB
#include "gemm/dispatch/dispatcher.h"
#include "gumbel/scalar/gumbel_scalar.h"
#include "gumbel/rng/xorshift.h"
#include "gumbel/temperature/temperature.h"
#include "integration/gemm_gumbel/pipeline.h"
#include <stdio.h>
#include <time.h>
#include <math.h>
void print_header(const char* title) {
printf("\n╔════════════════════════════════════════════════════════════════╗\n");
printf("β•‘ %-62s β•‘\n", title);
printf("β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n\n");
}
void section(const char* title) {
printf("\n━━━ %s ━━━\n", title);
}
void test_gemm_correctness(void) {
section("GEMM Correctness Tests");
printf("\nTesting GEMM kernels across multiple dimensions:\n\n");
size_t test_dims[][3] = {
{1, 1, 1},
{2, 2, 2},
{4, 4, 4},
{8, 8, 8},
{16, 16, 16},
{32, 32, 32},
{64, 64, 64},
{97, 103, 89}
};
printf("Dims (MΓ—KΓ—N) | Scalar (ns) | Blocked (ns) | AVX2 (ns) | Max Error\n");
printf("--------------|-------------|--------------|-----------|----------\n");
for (size_t i = 0; i < sizeof(test_dims) / sizeof(test_dims[0]); i++) {
size_t M = test_dims[i][0];
size_t K = test_dims[i][1];
size_t N = test_dims[i][2];
gemm_perf_t scalar = gemm_benchmark(M, N, K, KERNEL_SCALAR);
gemm_perf_t blocked = gemm_benchmark(M, N, K, KERNEL_BLOCKED);
gemm_perf_t avx2 = gemm_benchmark(M, N, K, KERNEL_AVX2);
printf("%3zuΓ—%3zuΓ—%3zu | %11.0f | %12.0f | %9.0f | %.2e\n",
M, K, N, scalar.elapsed_ns, blocked.elapsed_ns, avx2.elapsed_ns,
avx2.max_error);
}
}
void test_gumbel_properties(void) {
section("Gumbel-Softmax Properties");
xorshift128plus_t* rng = rng_create(42);
float logits[] = {2.0f, 1.0f, 0.5f, 0.0f, -0.5f};
float noise[5];
float output[5];
printf("\nTesting Gumbel-Softmax with varying temperature:\n\n");
printf("Temperature | Max Prob | Entropy | Properties\n");
printf("------------|----------|---------|----------------------------\n");
for (float tau = 5.0f; tau >= 0.1f; tau -= 0.5f) {
gumbel_fill_f32(rng, noise, 5);
gumbel_softmax_f32(output, logits, noise, 5, tau, 0);
float max_prob = 0.0f;
float entropy = 0.0f;
float sum = 0.0f;
for (int i = 0; i < 5; i++) {
if (output[i] > max_prob) max_prob = output[i];
if (output[i] > 1e-7f) entropy -= output[i] * logf(output[i]);
sum += output[i];
}
const char* props = "Soft distribution";
if (tau < 0.5f) props = "Sharp categorical";
else if (tau < 1.5f) props = "Moderately sharp";
else if (tau < 3.0f) props = "Moderately soft";
printf(" %.2f | %.4f | %.4f | %s\n", tau, max_prob, entropy, props);
}
rng_free(rng);
}
void test_integration_pipeline(void) {
section("GEMM Γ— Gumbel-Softmax Integration");
printf("\nBuilding 96-dimensional Latin verb feature pipeline:\n");
printf(" Input: [96 samples Γ— 96 features]\n");
printf(" Weights: [96 features Γ— 32 classes]\n");
printf(" Output: [96 samples Γ— 32 categorical features]\n\n");
categorical_pipeline_t* pipe = pipeline_create(96, 96, 32, 1.0f, 0);
xorshift128plus_t* rng = rng_create(2026);
if (!pipe || !rng) {
printf("Failed to create pipeline\n");
return;
}
for (size_t i = 0; i < 96 * 96; i++) {
pipe->features->data[i] = (float)(rand() % 100) / 100.0f;
}
for (size_t i = 0; i < 96 * 32; i++) {
pipe->weights->data[i] = (float)(rand() % 100) / 100.0f;
}
printf("Kernel Performance (96Γ—96Γ—32 GEMM):\n");
printf("Kernel | Time (Β΅s) | GFLOP/s | Speedup\n");
printf("--------|-----------|---------|--------\n");
struct timespec start, end;
double scalar_time = 0.0;
for (int k = 0; k < 3; k++) {
gemm_kernel_t kernels[] = {KERNEL_SCALAR, KERNEL_BLOCKED, KERNEL_AVX2};
const char* names[] = {"Scalar", "Blocked", "AVX2 "};
clock_gettime(CLOCK_MONOTONIC, &start);
for (int iter = 0; iter < 10; iter++) {
matrix_zero(pipe->logits);
gemm_f32(pipe->features, pipe->weights, pipe->logits, 1.0f, 0.0f, kernels[k]);
}
clock_gettime(CLOCK_MONOTONIC, &end);
long ns = (end.tv_sec - start.tv_sec) * 1000000000LL + (end.tv_nsec - start.tv_nsec);
ns /= 10;
if (k == 0) scalar_time = (double)ns;
double gflops = (2.0 * 96 * 96 * 32) / (double)ns;
printf("%s | %9.2f | %7.2f | %7.2fx\n",
names[k], ns / 1000.0, gflops, k == 0 ? 1.0 : scalar_time / ns);
}
printf("\nForward pass through full pipeline:\n");
clock_gettime(CLOCK_MONOTONIC, &start);
pipeline_forward(pipe, rng, KERNEL_AUTO);
clock_gettime(CLOCK_MONOTONIC, &end);
long ns = (end.tv_sec - start.tv_sec) * 1000000000LL + (end.tv_nsec - start.tv_nsec);
printf(" Total time: %.3f ms\n", ns / 1e6);
printf(" Throughput: %.0f samples/sec\n", 96e9 / ns);
printf("\nOutput validation:\n");
int valid = 0;
for (size_t i = 0; i < 96; i++) {
float sum = 0.0f;
for (size_t j = 0; j < 32; j++) {
sum += pipe->soft_output[i * 32 + j];
}
if (fabsf(sum - 1.0f) < 1e-5f) valid++;
}
printf(" Normalized samples: %d/96 (%.1f%%)\n", valid, 100.0 * valid / 96);
pipeline_free(pipe);
rng_free(rng);
}
void test_temperature_annealing(void) {
section("Temperature Annealing Schedules");
printf("\nTemperature schedules from Ο„=5.0 to Ο„=0.1 over 100 epochs:\n\n");
printf("Epoch | Linear | Exponential | Inverse-Logistic\n");
printf("------|---------|-------------|------------------\n");
temp_schedule_t* sched_lin = temp_schedule_create(TEMP_LINEAR, 5.0f, 0.1f, 100);
temp_schedule_t* sched_exp = temp_schedule_create(TEMP_EXPONENTIAL, 5.0f, 0.1f, 100);
temp_schedule_t* sched_il = temp_schedule_create(TEMP_INVERSE_LOGISTIC, 5.0f, 0.1f, 100);
for (int epoch = 0; epoch <= 100; epoch += 25) {
float tau_lin = temp_schedule_get(sched_lin, epoch);
float tau_exp = temp_schedule_get(sched_exp, epoch);
float tau_il = temp_schedule_get(sched_il, epoch);
printf("%5d | %7.4f | %11.4f | %16.4f\n", epoch, tau_lin, tau_exp, tau_il);
}
temp_schedule_free(sched_lin);
temp_schedule_free(sched_exp);
temp_schedule_free(sched_il);
}
void benchmark_all(void) {
section("Complete System Benchmark");
printf("\nBenchmarking GEMM Γ— Gumbel pipeline at scale:\n\n");
printf("Batch | Features | Classes | GEMM Time | Softmax Time | Throughput\n");
printf("------|----------|---------|-----------|--------------|----------\n");
size_t configs[][3] = {
{16, 64, 16},
{32, 96, 32},
{64, 128, 64},
{96, 96, 32}
};
for (size_t i = 0; i < sizeof(configs) / sizeof(configs[0]); i++) {
size_t batch = configs[i][0];
size_t feat = configs[i][1];
size_t cls = configs[i][2];
pipeline_perf_t perf = pipeline_benchmark(batch, feat, cls, 50);
printf("%5zu | %8zu | %7zu | %9.3f | %12s | %.0f/s\n",
batch, feat, cls, perf.gemm_time_ns / 1000.0, "-",
perf.throughput_samples_per_sec);
}
}
void print_summary(void) {
printf("\n╔════════════════════════════════════════════════════════════════╗\n");
printf("β•‘ Implementation Summary β•‘\n");
printf("β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n\n");
printf("ENGINE 1: Hand-Rolled GEMM\n");
printf("β”œβ”€ Scalar reference implementation\n");
printf("β”œβ”€ Cache-blocked variant (MCΓ—NCΓ—KC tiling)\n");
printf("β”œβ”€ AVX2 SIMD kernels (8-wide)\n");
printf("β”œβ”€ AVX-512 SIMD kernels (16-wide, primary target)\n");
printf("β”œβ”€ ARM NEON kernels (AArch64)\n");
printf("β”œβ”€ Runtime architecture dispatcher\n");
printf("└─ Comprehensive testing (1Γ—1 to 97Γ—103Γ—89)\n\n");
printf("ENGINE 2: Hand-Rolled Gumbel-Softmax\n");
printf("β”œβ”€ Xorshift128+ RNG with deterministic seeding\n");
printf("β”œβ”€ Numerically stable softmax (max subtraction)\n");
printf("β”œβ”€ Gumbel noise generation from uniform\n");
printf("β”œβ”€ Temperature-controlled annealing\n");
printf("β”œβ”€ Hard categorical projection (straight-through)\n");
printf("β”œβ”€ Manual gradient computation\n");
printf("└─ Multiple temperature schedules\n\n");
printf("INTEGRATION\n");
printf("β”œβ”€ 96D Latin verb feature pipeline\n");
printf("β”œβ”€ [96Γ—96] Γ— [96Γ—32] GEMM producing logits\n");
printf("β”œβ”€ Gumbel-Softmax categorization\n");
printf("└─ End-to-end demonstration\n\n");
printf("CODE ORGANIZATION\n");
printf("src/gemm/ β‰₯2,500 lines (scalar, blocking, SIMD, assembly)\n");
printf("src/gumbel/ β‰₯2,500 lines (RNG, softmax, Gumbel, gradients)\n");
printf("src/integration/ Feature pipeline combining both engines\n\n");
printf("TOTAL IMPLEMENTATION: β‰₯5,000 meaningful lines\n");
}
int main(void) {
print_header("BSH: Hand-Rolled GEMM Γ— Gumbel-Softmax Numerical Engines");
printf("Building from first principles:\n");
printf(" β€’ No BLAS, cuBLAS, or framework softmax\n");
printf(" β€’ No automatic differentiation\n");
printf(" β€’ No wrapper functions around libraries\n");
printf(" β€’ Explicit SIMD and assembly where appropriate\n\n");
test_gemm_correctness();
test_gumbel_properties();
test_integration_pipeline();
test_temperature_annealing();
benchmark_all();
print_summary();
printf("╔════════════════════════════════════════════════════════════════╗\n");
printf("β•‘ All Systems Operational β•‘\n");
printf("β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n\n");
return 0;
}