diff --git a/docs/implementation-plan.md b/docs/implementation-plan.md index 8e1374b..04232f4 100644 --- a/docs/implementation-plan.md +++ b/docs/implementation-plan.md @@ -168,8 +168,11 @@ a named invariant that the native runtime cannot protect yet. lowering, and assembly plans. Use `Mimir.BufferSmoke --perfect-machine-profile-smoke` to prove the catalog assembles, `--perfect-machine-contract-smoke` to write a CultCache contract - proof, and `--perfect-machine-manifest` to export the module manifest for - tooling/UI/remote witness use. + proof, `--perfect-machine-manifest` to export the module manifest for + tooling/UI/remote witness use, and `--perfect-machine-lowering-benchmark` to + measure the Mimir-to-Fensalir lowering path. The current six-camera/two-audio + synthetic lowering benchmark runs at roughly 2.5 us per iteration with about + 2.3 KB allocated per iteration. - `MimirVideoFrameDescriptor` for dimensions, pixel format, stride, device timestamp, and native/GPU handle metadata. - `IMimirVideoCaptureDriver` and `MimirVideoCaptureDriverSource` as the live diff --git a/docs/perfect-machine-module-library.md b/docs/perfect-machine-module-library.md index c16ce0c..a592eb1 100644 --- a/docs/perfect-machine-module-library.md +++ b/docs/perfect-machine-module-library.md @@ -49,6 +49,7 @@ Use these as quick probes after touching the module shelf: ```powershell dotnet build .\Mimir.slnx dotnet run --no-build --project .\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj -- --perfect-machine-profile-smoke +dotnet run --no-build --project .\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj -- --perfect-machine-lowering-benchmark dotnet run --no-build --project .\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj -- --perfect-machine-contract-smoke dotnet run --no-build --project .\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj -- --perfect-machine-manifest dotnet run --no-build --project .\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj -- --bioacoustic-actuator-self-test diff --git a/src/Mimir.BufferSmoke/Program.cs b/src/Mimir.BufferSmoke/Program.cs index 8d5019b..d7f1969 100644 --- a/src/Mimir.BufferSmoke/Program.cs +++ b/src/Mimir.BufferSmoke/Program.cs @@ -70,6 +70,12 @@ if (args.Any(arg => string.Equals(arg, "--perfect-machine-manifest", StringCompa .ConfigureAwait(false); } +if (args.Any(arg => string.Equals(arg, "--perfect-machine-lowering-benchmark", StringComparison.OrdinalIgnoreCase))) +{ + return RunPerfectMachineLoweringBenchmark( + ParseIntOption(args, "--iterations", 10_000)); +} + if (args.Any(arg => string.Equals(arg, "--standalone-chirp-bin-self-test", StringComparison.OrdinalIgnoreCase))) { return RunStandaloneChirpBinSelfTest( @@ -1187,6 +1193,90 @@ static async Task WritePerfectMachineManifestAsync(string outputPath) : 1; } +static int RunPerfectMachineLoweringBenchmark(int iterations) +{ + iterations = Math.Max(1, iterations); + var buffers = new List(); + foreach (var profile in MimirNativeCaptureConfigurations.LocalSixCameraProfiles) + { + var buffer = new MimirRollingStreamBuffer( + new MimirStreamDescriptor(profile.Id, MimirStreamKind.Video, MimirStreamOrigin.LocalDevice), + TimeSpan.FromSeconds(profile.RollingBufferSeconds)); + buffer.Append(new MimirStreamSample( + profile.Id, + MimirStreamKind.Video, + MimirStreamOrigin.LocalDevice, + TimestampNs: 1_000_000_000L, + ArrivalNs: 1_000_000_050L, + Sequence: 1, + PayloadHandle: 0, + VideoFrame: new MimirVideoFrameDescriptor( + Math.Max(1, profile.PreferredWidth), + Math.Max(1, profile.PreferredHeight), + profile.Id.Contains("leap", StringComparison.OrdinalIgnoreCase) ? MimirVideoPixelFormat.LeapStereoIr : MimirVideoPixelFormat.Bgra8, + Math.Max(1, profile.PreferredWidth) * 4, + 1_000_000_000L, + NativeHandle: (ulong)(100 + buffers.Count), + NativeHandleKind: "benchmark-shared-texture"))); + buffers.Add(buffer); + } + + var states = new[] + { + new MimirAudioSynchronizationState( + "loopback-scarlett-speakers", + "scarlett-input-1", + 192_000, + 41.50, + 41.50, + 0.216, + 0.30, + 0.91, + [], + 1_000_000_000L, + 1, + 1), + new MimirAudioSynchronizationState( + "loopback-scarlett-speakers", + "raven-scarlett-input-1", + 192_000, + 225.00, + 225.00, + 1.172, + -0.45, + 0.78, + [], + 1_000_000_000L, + 1, + 1) + }; + var lowerer = new MimirFensalirFieldLowering(); + _ = lowerer.BuildGpuSensorFrame(buffers); + _ = lowerer.BuildAcousticFieldFrame(states); + + var allocatedBefore = GC.GetAllocatedBytesForCurrentThread(); + var stamp = Stopwatch.GetTimestamp(); + var cameras = 0; + var constraints = 0; + for (var iteration = 0; iteration < iterations; iteration++) + { + var gpuFrame = lowerer.BuildGpuSensorFrame(buffers); + var acousticFrame = lowerer.BuildAcousticFieldFrame(states); + cameras += gpuFrame.Cameras.Count; + constraints += acousticFrame.Constraints.Count; + } + + var elapsed = Stopwatch.GetElapsedTime(stamp); + var allocatedBytes = GC.GetAllocatedBytesForCurrentThread() - allocatedBefore; + var perIterationUs = elapsed.TotalMilliseconds * 1000.0 / iterations; + var allocatedPerIteration = allocatedBytes / (double)iterations; + Console.WriteLine( + $"perfect-machine-lowering-benchmark iterations={iterations} camerasPer={cameras / (double)iterations:0.0} constraintsPer={constraints / (double)iterations:0.0} elapsedMs={elapsed.TotalMilliseconds:0.000} perIterationUs={perIterationUs:0.000} allocatedPerIterationBytes={allocatedPerIteration:0.0}"); + return perIterationUs < 250.0 && allocatedPerIteration < 10_000.0 + ? 0 + : 1; +} + static int RenderBioacousticFloat32(string outputPath, int sampleRate, double seconds) { var segmentCount = Math.Max(1, (int)Math.Ceiling(seconds / MimirBioacousticTimeline.SegmentSeconds)); diff --git a/src/Mimir.Runtime/Synchronization/MimirFensalirFieldLowering.cs b/src/Mimir.Runtime/Synchronization/MimirFensalirFieldLowering.cs index 4902a2c..b918d94 100644 --- a/src/Mimir.Runtime/Synchronization/MimirFensalirFieldLowering.cs +++ b/src/Mimir.Runtime/Synchronization/MimirFensalirFieldLowering.cs @@ -14,8 +14,9 @@ public sealed class MimirFensalirFieldLowering(MimirFensalirLoweringOptions? opt public AquariumGpuSensorFrame BuildGpuSensorFrame(IEnumerable buffers) { - var textures = new List(); - var cameras = new List(); + var capacity = buffers.TryGetNonEnumeratedCount(out var count) ? count : 0; + var textures = new List(capacity); + var cameras = new List(capacity); foreach (var buffer in buffers) { if (buffer.Descriptor.Kind != MimirStreamKind.Video || buffer.Latest?.VideoFrame is not { } frame) @@ -62,21 +63,39 @@ public sealed class MimirFensalirFieldLowering(MimirFensalirLoweringOptions? opt public AquariumAcousticFieldFrame BuildAcousticFieldFrame(IEnumerable states) { - var orderedStates = states - .OrderByDescending(state => state.Confidence) - .ThenBy(state => state.SourceId, StringComparer.Ordinal) - .ToArray(); - var constraints = orderedStates - .Where(state => state.Confidence > 0.0) - .Select(state => new AquariumAcousticConstraint( + var capacity = states.TryGetNonEnumeratedCount(out var count) ? count : 0; + var orderedStates = new List(capacity); + foreach (var state in states) + { + orderedStates.Add(state); + } + + orderedStates.Sort(static (left, right) => + { + var confidence = right.Confidence.CompareTo(left.Confidence); + return confidence != 0 + ? confidence + : string.Compare(left.SourceId, right.SourceId, StringComparison.Ordinal); + }); + + var constraints = new List(orderedStates.Count); + foreach (var state in orderedStates) + { + if (state.Confidence <= 0.0) + { + continue; + } + + constraints.Add(new AquariumAcousticConstraint( $"{state.ReferenceSourceId}->{state.SourceId}", AquariumAcousticConstraintKind.SpeakerProbe, Vector3.Zero, Vector3.Zero, RadiusMeters: 0.10f, Confidence: (float)Math.Clamp(state.Confidence, 0.0, 1.0), - TimestampNs: state.UpdatedAtNs)) - .ToArray(); + TimestampNs: state.UpdatedAtNs)); + } + var oracle = orderedStates.FirstOrDefault(); return new AquariumAcousticFieldFrame { diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 4028114..6767a9e 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -87,3 +87,4 @@ {"ts": "2026-05-24T16:06:07+01:00", "type": "implementation", "status": "ok", "note": "Added global bioacoustic clock hypothesis scoring to the training harness and the first Faust-owned alignment actuator surface. Corrected receipt artifacts/bioacoustic-training/bioacoustic-20260524-150000 favors baseline-mfcc-index/blur-light with total 0.766 after penalizing low-anchor timing fits. bioacoustic-actuator-self-test estimates 317.375 samples, applies fractional correction, and remeasures 0.000 us residual; faust/mimir_alignment_actuator.dsp validates through Faust C++ generation."} {"ts": "2026-05-24T16:09:30+01:00", "type": "implementation", "status": "ok", "note": "Extended the corrected bioacoustic training run to 0.75s in artifacts/bioacoustic-training/bioacoustic-20260524-150729. Compact indexing wins clean throughput at total 0.752/realtime 6.6x, baseline remains steadier under degradation, highband has warped-path value, and anchor density/time-to-lock should become explicit metrics."} {"ts": "2026-05-24T17:02:15+01:00", "type": "implementation", "status": "ok", "note": "Promoted Perfect Machine research configurations into Mimir.Runtime module library: decoder/language/path-learning/localization/benchmark/actuator/capture/ingest/reservoir/distributed/network/authority/publication profiles, CultMesh contracts, Fensalir lowering, manifest export, and BufferSmoke profile/contract smoke commands."} +{"ts": "2026-05-24T17:06:40+01:00", "type": "verification", "status": "ok", "note": "Added and optimized Mimir-to-Fensalir lowering benchmark. Six synthetic camera buffers plus two audio sync states lower in about 2.5 us/iteration with roughly 2.3 KB allocated per iteration after pre-sizing lists and removing LINQ sort/projection."}