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
38 changes: 26 additions & 12 deletions Bench/Micro/spatial_query_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ struct Lcg {
}
};

struct TimingSummary {
std::uint64_t p50 = 0;
std::uint64_t p95 = 0;
std::uint64_t p99 = 0;
};

template <typename Function>
std::uint64_t MedianNanoseconds(Function&& function)
TimingSummary MeasureNanoseconds(Function&& function)
{
std::array<std::uint64_t, 11> samples{};
std::array<std::uint64_t, 31> samples{};
std::uint64_t checksum = 0;
for (std::size_t i = 0; i < samples.size(); ++i)
{
Expand All @@ -40,10 +46,14 @@ std::uint64_t MedianNanoseconds(Function&& function)
std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count());
}
std::sort(samples.begin(), samples.end());
// Keep observable work outside the timed region.
if (checksum == 0xFFFFFFFFFFFFFFFFull)
std::cerr << "checksum sentinel\n";
return samples[samples.size() / 2];

const auto percentile = [&](std::size_t numerator) {
const std::size_t rank = (samples.size() * numerator + 99u) / 100u;
return samples[std::min(rank == 0 ? 0u : rank - 1u, samples.size() - 1u)];
};
return {percentile(50), percentile(95), percentile(99)};
}

std::uint64_t RunQueries(const std::vector<float>& xy,
Expand Down Expand Up @@ -123,9 +133,11 @@ int main()
constexpr float radius = 8.0f;
constexpr float cell_size = 4.0f;

std::cout << "# synthetic calibration only; do not treat CI timing as certification\n";
std::cout << "points,queries,cell_size,radius,baseline_total_ns,grid_stable_total_ns,"
"grid_unordered_total_ns\n";
std::cout << "# profile=spatial.synthetic.v1 seed=0x12345678 samples=31\n";
std::cout << "# synthetic calibration only; CI timing is never certification\n";
std::cout << "points,queries,cell_size,radius,baseline_p50_ns,baseline_p95_ns,baseline_p99_ns,"
"grid_stable_p50_ns,grid_stable_p95_ns,grid_stable_p99_ns,"
"grid_unordered_p50_ns,grid_unordered_p95_ns,grid_unordered_p99_ns\n";

for (const std::size_t point_count : sizes)
{
Expand All @@ -148,19 +160,21 @@ int main()
return 2;
}

const auto baseline_ns = MedianNanoseconds(
[&] { return RunQueries(xy, centers, radius, baseline); });
const auto stable_ns = MedianNanoseconds([&] {
const TimingSummary baseline_ns =
MeasureNanoseconds([&] { return RunQueries(xy, centers, radius, baseline); });
const TimingSummary stable_ns = MeasureNanoseconds([&] {
return RunGridQueries(grid, xy, centers, radius, candidate,
sc2opt::kernel::hot::SpatialOrder::InputOrder);
});
const auto unordered_ns = MedianNanoseconds([&] {
const TimingSummary unordered_ns = MeasureNanoseconds([&] {
return RunGridQueries(grid, xy, centers, radius, candidate,
sc2opt::kernel::hot::SpatialOrder::Unordered);
});

std::cout << point_count << ',' << query_count << ',' << cell_size << ',' << radius << ','
<< baseline_ns << ',' << stable_ns << ',' << unordered_ns << '\n';
<< baseline_ns.p50 << ',' << baseline_ns.p95 << ',' << baseline_ns.p99 << ','
<< stable_ns.p50 << ',' << stable_ns.p95 << ',' << stable_ns.p99 << ','
<< unordered_ns.p50 << ',' << unordered_ns.p95 << ',' << unordered_ns.p99 << '\n';
}

return 0;
Expand Down
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ target_link_libraries(sc2opt INTERFACE
sc2opt_adapters
)

if(SC2OPT_BUILD_TESTS OR SC2OPT_BUILD_BENCHMARKS)
add_library(sc2opt_proof STATIC
Proof/Profiles.cpp
Proof/Manifest.cpp
)
add_library(sc2opt::proof ALIAS sc2opt_proof)
target_include_directories(sc2opt_proof PUBLIC include)
target_link_libraries(sc2opt_proof PUBLIC sc2opt_tuner)
target_compile_features(sc2opt_proof PUBLIC cxx_std_20)
sc2opt_apply_warnings(sc2opt_proof)
endif()

if(SC2OPT_BUILD_PYTHON)
find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)
set(PYBIND11_FINDPYTHON ON)
Expand Down Expand Up @@ -130,6 +142,18 @@ if(SC2OPT_BUILD_TESTS)
sc2opt_apply_warnings(sc2opt_runtime_optimization_tests)
add_test(NAME sc2opt_runtime_optimization_tests COMMAND sc2opt_runtime_optimization_tests)

add_executable(sc2opt_deep_contract_tests tests/deep_contract_tests.cpp)
target_link_libraries(sc2opt_deep_contract_tests PRIVATE sc2opt)
target_compile_features(sc2opt_deep_contract_tests PRIVATE cxx_std_20)
sc2opt_apply_warnings(sc2opt_deep_contract_tests)
add_test(NAME sc2opt_deep_contract_tests COMMAND sc2opt_deep_contract_tests)

add_executable(sc2opt_manifest_tests tests/manifest_tests.cpp)
target_link_libraries(sc2opt_manifest_tests PRIVATE sc2opt_proof)
target_compile_features(sc2opt_manifest_tests PRIVATE cxx_std_20)
sc2opt_apply_warnings(sc2opt_manifest_tests)
add_test(NAME sc2opt_manifest_tests COMMAND sc2opt_manifest_tests)

add_executable(sc2opt_synthetic_cpp_consumer examples/synthetic_cpp_consumer.cpp)
target_link_libraries(sc2opt_synthetic_cpp_consumer PRIVATE sc2opt)
target_compile_features(sc2opt_synthetic_cpp_consumer PRIVATE cxx_std_20)
Expand Down
10 changes: 10 additions & 0 deletions Outputs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@
Generated benchmark, tuning, hardware/workload and certification manifests live here when explicitly produced for a run/release. They are not source authority and are git-ignored by default unless a reviewed fixture/baseline is intentionally committed.

Every consumable output must carry enough provenance to answer: what source/build produced this, on what hardware/workload, with what correctness status and confidence?

S30 provides a standard-library JSON writer plus `schemas/sc2aiopt-certification-v1.schema.json`. The writer fails closed when proof layers skip required gates, a synthetic workload attempts to claim representative proof, benchmark records are malformed, or identity/provenance is missing.

Proof layers are ordered:

```text
source/design -> build/tests -> representative benchmark -> consumer integration -> ladder/runtime
```

A later layer cannot be asserted without all earlier layers. This keeps CI/build success from being misrepresented as runtime certification.
7 changes: 5 additions & 2 deletions Profiles/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Profiles

Profiles describe the environment/workload under which a candidate was measured: CPU/hardware capabilities, OS, compiler, flags, source revision, workload identity and relevant SC2 build/data version.
Hardware and workload profiles make benchmark evidence contextual rather than universal.

Profiles are evidence inputs/outputs. They are not permission to silently enable hardware-specific code; capability and net-benefit gates still apply.
- `spatial.synthetic.v1.json` is a deterministic synthetic calibration workload. It is useful for equivalence and rough crossover discovery but **cannot** claim representative benchmark proof.
- `representative.template.json` defines the minimum identity shape for a consumer-supplied replay/game/workload corpus. The template itself is not evidence and must never be treated as certified data.

Representative profiles should identify the exact corpus (preferably a content hash), SC2 build/data version, selection rule and workload family. Hardware identity belongs in generated evidence/manifests rather than being hard-coded as source truth.
16 changes: 16 additions & 0 deletions Profiles/representative.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"schema_version": 1,
"id": "REPLACE_ME",
"family": "REPLACE_ME",
"version": "1",
"kind": "representative",
"sc2_build": "75689",
"data_version": "B89B5D6FA7CBF6452E721311BFBC6CB2",
"corpus_identity": "sha256:REPLACE_ME",
"source": {
"description": "Representative replay/game/workload corpus supplied by the consumer proof flow",
"selection_rule": "REPLACE_ME",
"cases": []
},
"certification_role": "not-valid-until-all-placeholders-are-replaced-and-proven"
}
19 changes: 19 additions & 0 deletions Profiles/spatial.synthetic.v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"schema_version": 1,
"id": "spatial.synthetic.v1",
"family": "spatial-radius-query",
"version": "1",
"kind": "synthetic",
"sc2_build": "75689",
"data_version": "B89B5D6FA7CBF6452E721311BFBC6CB2",
"corpus_identity": "generator:lcg-1664525-1013904223;seed:0x12345678",
"generator": {
"coordinate_domain": [0.0, 128.0],
"point_counts": [8, 16, 32, 64, 128, 256, 512],
"query_count": 256,
"radius": 8.0,
"cell_size": 4.0,
"timing_samples": 31
},
"certification_role": "synthetic-calibration-only"
}
163 changes: 163 additions & 0 deletions Proof/Manifest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#include "sc2opt/proof/Manifest.hpp"

#include <cmath>
#include <iomanip>
#include <ostream>

namespace sc2opt::proof {
namespace {

bool BenchmarkValid(const BenchmarkRecord& record) noexcept
{
return record.samples > 0 && std::isfinite(record.mean_total_cost_ns) &&
record.mean_total_cost_ns >= 0.0 && record.p50_ns <= record.p95_ns &&
record.p95_ns <= record.p99_ns;
}

bool ProofOrderValid(const ProofLayers& proof) noexcept
{
if (proof.build_tests && !proof.source_design)
return false;
if (proof.representative_benchmark && !proof.build_tests)
return false;
if (proof.consumer_integration && !proof.representative_benchmark)
return false;
if (proof.ladder_runtime && !proof.consumer_integration)
return false;
return true;
}

void WriteJsonString(std::ostream& out, std::string_view value)
{
static constexpr char hex[] = "0123456789abcdef";
out.put('"');
for (const unsigned char ch : value)
{
switch (ch)
{
case '"': out << "\\\""; break;
case '\\': out << "\\\\"; break;
case '\b': out << "\\b"; break;
case '\f': out << "\\f"; break;
case '\n': out << "\\n"; break;
case '\r': out << "\\r"; break;
case '\t': out << "\\t"; break;
default:
if (ch < 0x20u)
{
out << "\\u00" << hex[(ch >> 4u) & 0x0fu] << hex[ch & 0x0fu];
}
else
{
out.put(static_cast<char>(ch));
}
break;
}
}
out.put('"');
}

void WriteBool(std::ostream& out, bool value)
{
out << (value ? "true" : "false");
}

} // namespace

ManifestIssue ValidateManifest(const CertificationManifestView& manifest) noexcept
{
if (manifest.schema_version != kCertificationManifestSchemaVersion)
return ManifestIssue::SchemaVersion;
if (manifest.source_revision.empty() || manifest.generated_utc.empty())
return ManifestIssue::MissingIdentity;
if (!IsValid(manifest.hardware))
return ManifestIssue::InvalidHardware;
if (!IsValid(manifest.workload))
return ManifestIssue::InvalidWorkload;
if (!ProofOrderValid(manifest.proof))
return ManifestIssue::InvalidProofOrder;
if (manifest.proof.representative_benchmark &&
(manifest.workload.kind != WorkloadKind::Representative || manifest.benchmarks.empty()))
{
return ManifestIssue::MissingRepresentativeEvidence;
}

for (const BenchmarkRecord& record : manifest.benchmarks)
{
if (!BenchmarkValid(record))
return ManifestIssue::InvalidBenchmark;
if (manifest.proof.representative_benchmark && !record.correctness_passed)
return ManifestIssue::InvalidBenchmark;
}

return ManifestIssue::None;
}

bool WriteCertificationManifestJson(std::ostream& out,
const CertificationManifestView& manifest)
{
if (ValidateManifest(manifest) != ManifestIssue::None)
return false;

out << "{\n \"schema_version\": " << manifest.schema_version << ",\n \"source_revision\": ";
WriteJsonString(out, manifest.source_revision);
out << ",\n \"generated_utc\": ";
WriteJsonString(out, manifest.generated_utc);
out << ",\n \"hardware\": {\n \"id\": ";
WriteJsonString(out, manifest.hardware.id);
out << ",\n \"cpu\": ";
WriteJsonString(out, manifest.hardware.cpu);
out << ",\n \"os\": ";
WriteJsonString(out, manifest.hardware.os);
out << ",\n \"compiler\": ";
WriteJsonString(out, manifest.hardware.compiler);
out << ",\n \"build_config\": ";
WriteJsonString(out, manifest.hardware.build_config);
out << "\n },\n \"workload\": {\n \"id\": ";
WriteJsonString(out, manifest.workload.id);
out << ",\n \"family\": ";
WriteJsonString(out, manifest.workload.family);
out << ",\n \"version\": ";
WriteJsonString(out, manifest.workload.version);
out << ",\n \"kind\": ";
WriteJsonString(out, manifest.workload.kind == WorkloadKind::Representative ? "representative"
: "synthetic");
out << ",\n \"sc2_build\": ";
WriteJsonString(out, manifest.workload.sc2_build);
out << ",\n \"data_version\": ";
WriteJsonString(out, manifest.workload.data_version);
out << ",\n \"corpus_identity\": ";
WriteJsonString(out, manifest.workload.corpus_identity);
out << "\n },\n \"proof\": {\n \"source_design\": ";
WriteBool(out, manifest.proof.source_design);
out << ",\n \"build_tests\": ";
WriteBool(out, manifest.proof.build_tests);
out << ",\n \"representative_benchmark\": ";
WriteBool(out, manifest.proof.representative_benchmark);
out << ",\n \"consumer_integration\": ";
WriteBool(out, manifest.proof.consumer_integration);
out << ",\n \"ladder_runtime\": ";
WriteBool(out, manifest.proof.ladder_runtime);
out << "\n },\n \"benchmarks\": [";

for (std::size_t i = 0; i < manifest.benchmarks.size(); ++i)
{
const BenchmarkRecord& record = manifest.benchmarks[i];
out << (i == 0 ? "\n" : ",\n") << " {\"candidate\": " << record.candidate
<< ", \"workload_size\": " << record.workload_size
<< ", \"samples\": " << record.samples
<< ", \"mean_total_cost_ns\": " << std::setprecision(17)
<< record.mean_total_cost_ns << ", \"p50_ns\": " << record.p50_ns
<< ", \"p95_ns\": " << record.p95_ns << ", \"p99_ns\": " << record.p99_ns
<< ", \"correctness_passed\": ";
WriteBool(out, record.correctness_passed);
out << '}';
}

if (!manifest.benchmarks.empty())
out << '\n';
out << " ]\n}\n";
return static_cast<bool>(out);
}

} // namespace sc2opt::proof
18 changes: 18 additions & 0 deletions Proof/Profiles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "sc2opt/proof/Profiles.hpp"

namespace sc2opt::proof {

bool IsValid(const HardwareProfileView& profile) noexcept
{
return !profile.id.empty() && !profile.cpu.empty() && !profile.os.empty() &&
!profile.compiler.empty() && !profile.build_config.empty();
}

bool IsValid(const WorkloadProfileView& profile) noexcept
{
return !profile.id.empty() && !profile.family.empty() && !profile.version.empty() &&
!profile.sc2_build.empty() && !profile.data_version.empty() &&
!profile.corpus_identity.empty();
}

} // namespace sc2opt::proof
35 changes: 35 additions & 0 deletions docs/PROOF_AND_OUTPUTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Proof and outputs — S28-S30

## S28 deep tests

The deterministic deep-contract suite adds property/fuzz-style coverage without a fuzzing runtime dependency:

- geometry symmetry and boundary invariants across thousands of generated cases;
- filtering equivalence against an independent reference predicate;
- spatial-grid exact InputOrder equivalence against brute force across generated worlds, cell sizes, radii and strict/inclusive boundaries;
- unordered spatial membership equivalence;
- non-finite input fail-closed regression coverage.

The generator seed is fixed so failures are reproducible in CI. Dedicated sanitizer/fuzzer toolchain integration remains S31.

## S29 benchmark/workload profiles

Workloads have explicit identity, SC2 build/data version, family, version, kind and corpus identity. Synthetic and representative evidence are different types of proof.

The spatial microbenchmark now records p50/p95/p99 from 31 samples and declares its deterministic profile/seed in output. CI timing remains synthetic calibration data and is explicitly not certification.

`Profiles/representative.template.json` is only a contract/template. It cannot become evidence until a real consumer corpus replaces all placeholders and receives a stable corpus identity/hash.

## S30 consumable outputs

`sc2opt_proof` is a separate proof-only library and is **not linked by `sc2opt::sc2opt`**. It validates hardware/workload identity and writes a versioned certification JSON manifest with benchmark records and proof-layer status.

The writer enforces monotonic proof layers:

```text
source/design -> build/tests -> representative benchmark -> consumer integration -> ladder/runtime
```

Representative benchmark proof additionally requires a representative workload and non-empty correctness-passing benchmark evidence. This prevents a green CI run or synthetic benchmark from being serialized as a stronger certification than it really is.

The JSON schema is published under `schemas/` for BotOps and other consumers.
Loading
Loading