strawberry/tests/model_score.cpp
trvon e5ee53cf18
All checks were successful
release / draft (push) Has been skipped
release / build (debian:trixie, trixie) (push) Successful in 56m31s
release / build (ubuntu:24.04, noble) (push) Successful in 51m33s
release / publish (push) Has been skipped
Review fixes: write->exec feature constant, accurate comments, tag-safe CI
Address code-review findings on this session's changes (no behavior change):
- Share the dense-slot index for the write->exec feature as
  learn::kWriteExecFeatureIndex; featurize() and the model-score dev tool now
  reference it instead of a duplicated literal 42 (prevents silent column drift
  if the feature layout shifts).
- Fix the self_written_execs comments: the linkage is a GLOBAL bounded FIFO
  (recent_writes), not strictly "this window" — the docs now match replay.cpp.
- release.yml: never cancel a tag/release run (cancel-in-progress only for
  non-tag refs); push ordering doesn't guarantee the tag event arrives after the
  branch event, so a same-sha branch push must not abort an in-progress release.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-10 16:09:03 -07:00

73 lines
2.8 KiB
C++

// strawberry-model-score: dev tool that applies a trained model (.sbm) to every window feature vector in an
// export bundle (.sbx) and prints TSV: window_id(12) value label features[42]. Summary goes to stderr.
// label: 1 positive, 0 negative, -1 absent. Not installed.
#include "strawberry/fleet_export.hpp"
#include "strawberry/learn/mlp.hpp"
#include "strawberry/learn/model_file.hpp"
#include <array>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <string_view>
#include <vector>
namespace strawberry {
namespace {
int run(int argc, char** argv) {
if (argc != 3 || argv[1][0] == '\0' || argv[2][0] == '\0') {
std::cerr << "usage: strawberry-model-score <bundle.sbx> <model.sbm>\n";
return 2;
}
learn::Model model;
std::string err;
if (!learn::load_model(argv[2], model, err)) {
std::cerr << "error: " << err << "\n";
return 1;
}
fleet::ExportManifest manifest;
std::map<std::string, std::array<float, fleet::kExportFeatureDim>> feats;
std::map<std::string, LabelPolarity> labels;
const bool ok = fleet::read_export_bundle(
argv[1], manifest, [&](uint32_t type, std::string_view payload) {
if (type == static_cast<uint32_t>(store::RecordType::export_window_feature)) {
fleet::ExportWindowFeature f;
if (fleet::decode_export_window_feature(payload, f)) feats[f.window_id] = f.features;
} else if (type == static_cast<uint32_t>(store::RecordType::export_label)) {
fleet::ExportLabel l;
if (fleet::decode_export_label(payload, l)) labels[l.window_id] = l.polarity;
}
});
if (!ok) {
std::cerr << "error: unreadable bundle\n";
return 1;
}
std::size_t n = 0, na = 0, nb = 0;
double sa = 0, sb = 0;
for (const auto& [id, f] : feats) {
const std::vector<float> x(f.begin(), f.end());
const learn::ModelOutput o = learn::forward(model, x);
int label = -1;
if (auto it = labels.find(id); it != labels.end()) label = (it->second == LabelPolarity::positive) ? 1 : 0;
std::printf("%s\t%.6f\t%d\t%.4f\n", id.substr(0, 12).c_str(), static_cast<double>(o.value), label,
static_cast<double>(f[learn::kWriteExecFeatureIndex]));
++n;
if (f[learn::kWriteExecFeatureIndex] > 0.0f) {
++na;
sa += o.value;
} else {
++nb;
sb += o.value;
}
}
std::fprintf(stderr, "# n=%zu | x42>0: n=%zu mean_value=%.6f | x42==0: n=%zu mean_value=%.6f\n", n, na,
na ? sa / static_cast<double>(na) : 0.0, nb, nb ? sb / static_cast<double>(nb) : 0.0);
return 0;
}
} // namespace
} // namespace strawberry
int main(int argc, char** argv) { return strawberry::run(argc, argv); }