Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,7 @@ MANIFEST

# OS
.DS_Store
Thumbs.db
Thumbs.db

# Node
node_modules/
2 changes: 1 addition & 1 deletion NOTICE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Sofia Engine
Copyright 2026 Rootcastle Engineering & Innovation
Project home: https://github.com/rootcastleco/sofia-rl
Project home: https://github.com/rootcastleco/sofia-ai

This product includes software developed by the Rootcastle Engineering &
Innovation team.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
[![Security Audited](https://img.shields.io/badge/security-threat%20modeled%20%7C%20no%20pickle-red.svg)](specs/001-sofia-engine-modernization/threat-model.md)
[![Organization](https://img.shields.io/badge/developed%20by-Rootcastle%20Engineering-black.svg)](https://rootcastle.com/)

[**Documentation**](https://rootcastleco.github.io/sofia-rl/) | [**GitHub Wiki**](https://github.com/rootcastleco/sofia-rl/wiki) | [**NPM Package**](https://www.npmjs.com/package/@rootcastle/sofia-engine) | [**REI SignalLab**](https://github.com/rootcastleco/rei-signallab) | [**Rootcastle**](https://rootcastle.com/)
[**Documentation**](https://rootcastleco.github.io/sofia-ai/) | [**GitHub Wiki**](https://github.com/rootcastleco/sofia-ai/wiki) | [**NPM Package**](https://www.npmjs.com/package/@rootcastle/sofia-engine) | [**REI SignalLab**](https://github.com/rootcastleco/rei-signallab) | [**Rootcastle**](https://rootcastle.com/)

---

Expand Down
5 changes: 3 additions & 2 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Report it privately to the maintainers instead:

- Open a private advisory: GitHub → *Security* → *Report a vulnerability*
- Or email the Rootcastle Engineering & Innovation team via the repository
maintainers with the subject `[sofia-rl security]`.
maintainers with the subject `[sofia-ai security]`.

Please include: module and version affected, a description of the weakness,
and — if you have one — a minimal reproduction. Coordinated disclosure is
Expand All @@ -24,7 +24,8 @@ active hardening but should still be disclosed through the same channel.

| Version | Supported |
| --- | --- |
| 2.x (sofia-engine) | Yes |
| 3.x (sofia-engine) | Yes |
| 2.x (sofia-engine) | Maintenance only |
| 1.x (legacy sofia_ai) | No — reference only |

## Design posture
Expand Down
83 changes: 83 additions & 0 deletions benchmarks/results/baseline_benchmark.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"timestamp_utc": "2026-09-21T23:56:41.289504+00:00",
"environment": {
"os": "Windows",
"os_release": "11",
"os_version": "10.0.26200",
"machine": "AMD64",
"processor": "Intel64 Family 6 Model 165 Stepping 2, GenuineIntel",
"python_version": "3.12.10",
"python_compiler": "MSC v.1943 64 bit (AMD64)",
"python_implementation": "CPython"
},
"benchmarks": {
"import_time_ms": 342.76,
"fft_psd_1024": {
"n_iterations": 100,
"mean_us": 319.19,
"p50_us": 307.55,
"p95_us": 362.82,
"p99_us": 445.46,
"min_us": 303.3,
"max_us": 501.2
},
"fft_psd_4096": {
"n_iterations": 100,
"mean_us": 430.25,
"p50_us": 387.35,
"p95_us": 597.33,
"p99_us": 713.05,
"min_us": 378.7,
"max_us": 787.2
},
"analytic_envelope_2048": {
"n_iterations": 100,
"mean_us": 140.95,
"p50_us": 127.8,
"p95_us": 200.21,
"p99_us": 237.93,
"min_us": 126.6,
"max_us": 241.2
},
"feature_extraction_1024": {
"n_iterations": 100,
"mean_us": 2521.47,
"p50_us": 2415.45,
"p95_us": 3304.84,
"p99_us": 3490.95,
"min_us": 2165.0,
"max_us": 3554.8
},
"electrical_symmetrical_components": {
"n_iterations": 100,
"mean_us": 60.68,
"p50_us": 55.5,
"p95_us": 92.99,
"p99_us": 102.7,
"min_us": 54.7,
"max_us": 103.0
},
"sofia_vm_forward_inference": {
"n_iterations": 100,
"mean_us": 1751.24,
"p50_us": 1675.8,
"p95_us": 2210.2,
Comment on lines +60 to +64
"p99_us": 2457.12,
"min_us": 1560.8,
"max_us": 2537.8
},
"sofia_vm_training_step": {
"n_iterations": 100,
"mean_us": 6609.69,
"p50_us": 6442.9,
"p95_us": 7483.03,
"p99_us": 9051.64,
"min_us": 6194.4,
"max_us": 9362.5
}
},
"memory": {
"current_traced_kb": 7116.01,
"peak_traced_kb": 7137.29
}
}
155 changes: 155 additions & 0 deletions benchmarks/run_benchmarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
"""Standardized Benchmark Runner for Sofia Engine Runtime.

Collects real, unembellished latency, throughput, and memory measurements on the
host environment in accordance with specs/sofia-runtime-v3/performance-budgets.md.
Zero fabricated numbers.
"""

from __future__ import annotations

import gc
import json
import os
import platform
import sys
Comment on lines +10 to +14
import time
import tracemalloc
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Callable

import numpy as np

REPO_ROOT = Path(__file__).resolve().parents[1]
RESULTS_DIR = REPO_ROOT / "benchmarks" / "results"


def measure_benchmark(
fn: Callable[[], Any],
n_iterations: int = 100,
warmup: int = 10,
) -> dict[str, float]:
"""Run warmup iterations and measure statistical latency percentiles."""
for _ in range(warmup):
fn()

durations: list[float] = []
for _ in range(n_iterations):
t0 = time.perf_counter_ns()
fn()
t1 = time.perf_counter_ns()
durations.append((t1 - t0) / 1000.0) # microseconds

durations.sort()
p50 = float(np.percentile(durations, 50))
p95 = float(np.percentile(durations, 95))
p99 = float(np.percentile(durations, 99))
mean = float(np.mean(durations))

return {
"n_iterations": n_iterations,
"mean_us": round(mean, 2),
"p50_us": round(p50, 2),
"p95_us": round(p95, 2),
"p99_us": round(p99, 2),
"min_us": round(durations[0], 2),
"max_us": round(durations[-1], 2),
}


def run_all_benchmarks() -> dict[str, Any]:
print("=" * 70)
print("SOFIA ENGINE RUNTIME — STANDARDIZED BENCHMARK RUNNER")
print("=" * 70)

tracemalloc.start()

# 1. Package import time
t0 = time.perf_counter()
import sofia_ai
import sofia_ai.signal.electrical as electrical
import sofia_ai.signal.envelope as envelope
import sofia_ai.signal.spectral as spectral
import sofia_ai.features as features
import sofia_ai.learning.asm as asm
import sofia_ai.learning.asm.neural as asm_neural
t1 = time.perf_counter()
import_time_ms = round((t1 - t0) * 1000.0, 2)
print(f"[*] Package import time: {import_time_ms} ms")

# 2. FFT / Welch PSD (1024 samples)
x_1024 = np.sin(2.0 * np.pi * 50.0 * np.linspace(0, 1, 1024, endpoint=False))
bench_psd_1024 = measure_benchmark(lambda: spectral.welch_psd(x_1024, sample_rate=1000.0, segment_length=256))
print(f"[*] FFT/PSD (1024 samples) p50: {bench_psd_1024['p50_us']} us")

# 3. FFT / Welch PSD (4096 samples)
x_4096 = np.sin(2.0 * np.pi * 50.0 * np.linspace(0, 4, 4096, endpoint=False))
bench_psd_4096 = measure_benchmark(lambda: spectral.welch_psd(x_4096, sample_rate=1000.0, segment_length=512))
print(f"[*] FFT/PSD (4096 samples) p50: {bench_psd_4096['p50_us']} us")

# 4. Hilbert Analytic Envelope (2048 samples)
x_2048 = (1.0 + 0.5 * np.sin(2.0 * np.pi * 10.0 * np.linspace(0, 1, 2048, endpoint=False))) * np.sin(2.0 * np.pi * 150.0 * np.linspace(0, 1, 2048, endpoint=False))
bench_envelope = measure_benchmark(lambda: envelope.amplitude_envelope(x_2048))
print(f"[*] Analytic Envelope (2048 samples) p50: {bench_envelope['p50_us']} us")

# 5. Full Statistical Feature Extraction (1024 samples)
bench_features = measure_benchmark(lambda: features.extract_from_array(x_1024, sample_rate=1000.0))
print(f"[*] Feature Extraction (1024 samples) p50: {bench_features['p50_us']} us")

# 6. Electrical Power Metrics & Symmetrical Components
bench_electrical = measure_benchmark(lambda: electrical.compute_symmetrical_components(230.0, 0.0, 210.0, -120.0, 200.0, 120.0))
print(f"[*] Electrical Symmetrical Components p50: {bench_electrical['p50_us']} us")

# 7. Sofia VM Forward Inference (Assembly Neural Network: 4 -> 8 -> 1)
ann = asm_neural.AssemblyNeuralNetwork(input_dim=4, hidden_dim=8, output_dim=1, learning_rate=0.01)
input_sample = np.array([0.5, -0.2, 0.8, 0.1], dtype=np.float64)
target_sample = np.array([0.75], dtype=np.float64)

bench_vm_inference = measure_benchmark(lambda: ann.forward(input_sample))
print(f"[*] Sofia VM Forward Inference (4->8->1) p50: {bench_vm_inference['p50_us']} us")

# 8. Sofia VM Training Step (Forward + Backprop + SGD)
bench_vm_train = measure_benchmark(lambda: ann.train_step(input_sample, target_sample))
print(f"[*] Sofia VM Training Step (4->8->1) p50: {bench_vm_train['p50_us']} us")

current_mem, peak_mem = tracemalloc.get_traced_memory()
tracemalloc.stop()

results = {
"timestamp_utc": datetime.now(timezone.utc).isoformat(),
"environment": {
"os": platform.system(),
"os_release": platform.release(),
"os_version": platform.version(),
"machine": platform.machine(),
"processor": platform.processor(),
"python_version": platform.python_version(),
"python_compiler": platform.python_compiler(),
"python_implementation": platform.python_implementation(),
},
"benchmarks": {
"import_time_ms": import_time_ms,
"fft_psd_1024": bench_psd_1024,
"fft_psd_4096": bench_psd_4096,
"analytic_envelope_2048": bench_envelope,
"feature_extraction_1024": bench_features,
"electrical_symmetrical_components": bench_electrical,
"sofia_vm_forward_inference": bench_vm_inference,
"sofia_vm_training_step": bench_vm_train,
},
"memory": {
"current_traced_kb": round(current_mem / 1024.0, 2),
"peak_traced_kb": round(peak_mem / 1024.0, 2),
},
}

RESULTS_DIR.mkdir(parents=True, exist_ok=True)
out_file = RESULTS_DIR / "baseline_benchmark.json"
out_file.write_text(json.dumps(results, indent=2) + "\n", encoding="utf-8")
print(f"[+] Saved baseline benchmark results to: {out_file}")
print("=" * 70)
return results


if __name__ == "__main__":
run_all_benchmarks()
34 changes: 34 additions & 0 deletions embedded/tests/test_sofia_features.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "../src/sofia_features.h"
#include "../src/sofia_fixed.h"
#include "golden_vectors.h"

#include <math.h>
#include <stdio.h>
Expand Down Expand Up @@ -188,6 +189,38 @@ static void test_q16_roundtrip(void)
}
}

static void test_golden_vectors(void)
{
sofia_workspace_t ws;
size_t c;
for (c = 0u; c < SOFIA_GOLDEN_N_CASES; ++c) {
const sofia_golden_case_t *gc = &sofia_golden_cases[c];
sofia_stat_features_t out;
sofia_status_t status;

status = sofia_features_init(&ws, 256);
check_true(status == SOFIA_OK, "golden init 256");

status = sofia_stat_features(gc->samples, gc->n, &out, &ws);
check_true(status == SOFIA_OK, "golden stat features ok");

check_close(out.mean, gc->expected[0], SOFIA_GOLDEN_TOLERANCE, "golden mean");
check_close(out.rms, gc->expected[1], SOFIA_GOLDEN_TOLERANCE, "golden rms");
check_close(out.peak, gc->expected[2], SOFIA_GOLDEN_TOLERANCE, "golden peak");
check_close(out.peak_to_peak, gc->expected[3], SOFIA_GOLDEN_TOLERANCE, "golden peak_to_peak");
check_close(out.variance, gc->expected[4], SOFIA_GOLDEN_TOLERANCE, "golden variance");
check_close(out.std, gc->expected[5], SOFIA_GOLDEN_TOLERANCE, "golden std");
check_close(out.crest_factor, gc->expected[6], SOFIA_GOLDEN_TOLERANCE, "golden crest_factor");
check_close(out.shape_factor, gc->expected[7], SOFIA_GOLDEN_TOLERANCE, "golden shape_factor");
check_close(out.impulse_factor, gc->expected[8], SOFIA_GOLDEN_TOLERANCE, "golden impulse_factor");
check_close(out.margin_factor, gc->expected[9], SOFIA_GOLDEN_TOLERANCE, "golden margin_factor");
check_close(out.skewness, gc->expected[10], SOFIA_GOLDEN_TOLERANCE, "golden skewness");
check_close(out.kurtosis, gc->expected[11], SOFIA_GOLDEN_TOLERANCE, "golden kurtosis");
check_close(out.zero_crossing_rate, gc->expected[12], SOFIA_GOLDEN_TOLERANCE, "golden zero_crossing_rate");
check_close(out.energy, gc->expected[13], SOFIA_GOLDEN_TOLERANCE, "golden energy");
}
}

int main(void)
{
printf("SOFIA embedded runtime tests\n");
Expand All @@ -198,6 +231,7 @@ int main(void)
test_spectral_features();
test_fixed_point();
test_q16_roundtrip();
test_golden_vectors();

printf("%d checks, %d failures\n", checks, failures);
if (failures != 0) {
Expand Down
6 changes: 3 additions & 3 deletions mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
site_name: Sofia Engine
site_description: Offline-First Edge Intelligence for Industrial Telemetry, Condition Monitoring & Safe Technical Automation
site_author: Rootcastle Engineering & Innovation
site_url: https://rootcastleco.github.io/sofia-rl/
repo_url: https://github.com/rootcastleco/sofia-rl
repo_name: rootcastleco/sofia-rl
site_url: https://rootcastleco.github.io/sofia-ai/
repo_url: https://github.com/rootcastleco/sofia-ai
repo_name: rootcastleco/sofia-ai

theme:
name: material
Expand Down
2 changes: 1 addition & 1 deletion packages/sofia-engine/NOTICE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Sofia Engine
Copyright 2026 Rootcastle Engineering & Innovation
Project home: https://github.com/rootcastleco/sofia-rl
Project home: https://github.com/rootcastleco/sofia-ai

This product includes software developed by the Rootcastle Engineering &
Innovation team.
Expand Down
2 changes: 1 addition & 1 deletion packages/sofia-engine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
[![Security Audited](https://img.shields.io/badge/security-threat%20modeled%20%7C%20no%20pickle-red.svg)](specs/001-sofia-engine-modernization/threat-model.md)
[![Organization](https://img.shields.io/badge/developed%20by-Rootcastle%20Engineering-black.svg)](https://rootcastle.com/)

[**Documentation**](https://rootcastleco.github.io/sofia-rl/) | [**GitHub Wiki**](https://github.com/rootcastleco/sofia-rl/wiki) | [**NPM Package**](https://www.npmjs.com/package/@rootcastle/sofia-engine) | [**REI SignalLab**](https://github.com/rootcastleco/rei-signallab) | [**Rootcastle**](https://rootcastle.com/)
[**Documentation**](https://rootcastleco.github.io/sofia-ai/) | [**GitHub Wiki**](https://github.com/rootcastleco/sofia-ai/wiki) | [**NPM Package**](https://www.npmjs.com/package/@rootcastle/sofia-engine) | [**REI SignalLab**](https://github.com/rootcastleco/rei-signallab) | [**Rootcastle**](https://rootcastle.com/)

---

Expand Down
Loading
Loading