memlnaut-nisps/nisps/ml/geo_push.hpp

96 lines
3.7 KiB
C++
Raw Normal View History

feat(ml)!: P3 core — geometric dislike in nisps/, jolt/OU + geo ABI Geometric dislike (rl-feedback-design §2.1/§4; upstream InterfaceRL @ 0a541cc ported verbatim, constants included): - nisps/ml/replay.hpp: ReplayView over storage-owned buffers — deepen-or- store negatives (dedup 0.05, clamp -16), k-NN positive centroid with deterministic index tie-break + fixed accumulation order, proportional decay (0.0025*max(|r|,1)) + eviction, order-preserving compaction. - nisps/ml/geo_push.hpp: push-away target (pushStep clamp(|avgNeg|,.25,1) *0.5, taper /(1+len), useRandom on len<=1e-4 via nisps::Rng — the single deliberate divergence from libc rand()), negLRRatio 0.5-0.4*negFraction. - mlp.hpp: train_targets(input, computed-target, lr, out_mask) — trains toward computed targets (negative lr = cold-start train-away); solo/ focus gating zeroes masked derivs. - feedback.hpp: AvoidStyle {Geometric (new default), Diffuse (legacy move_weights, kept for A/B)}; dislike_geometric() collapses upstream's press+optimise into one synchronous call; on_up in geometric Avoid feeds the positive centroid; dislike-multiplier bookkeeping. Storage gains replay buffers (Fixed: ReplayCap=32 firmware default ≈ +8KB SRAM; Dynamic arena: cap 64). - bindings: nisps_ml_feedback_{dislike_geometric,store_positive, positive_count,negative_count,set_avoid_style} + P3.2 jolt/OU ABI: nisps_ml_jolt_{press,step,release,active,lr_scale,tick_lr_ramp}, nisps_ml_explore_{intensity,get_intensity,apply} (OUNoise<4096> over-provisioned; same code the firmware ModeBase runs). - parity v4: Stage 6 scripted geometric session (2 likes → 2 dislikes, f32-exact heard vectors via Math.fround) — 961 floats PASS at 2.4e-7. - tests: test_mlp_geo_dislike.cpp (replay dedup/deepen/clamp, centroid tie-break, push direction/taper/mask/clamp, cold-start inertness + train-away, determinism, Diffuse legacy); legacy Avoid test pinned to Diffuse per the ADR's deliberate-break note. Firmware: PAFSynth .text/.data unchanged (geometric path not referenced by current glue). NOTE: discovered pre-existing bug 10c3e55c — the explore/place wiring is linker-GC'd out of the PAFSynth ELF (predates this refactor; evidence in the ergo task).
2026-07-14 04:16:21 +02:00
// nisps/ml/geo_push.hpp — geometric push-away target computation for the
// dislike gesture (docs/adr/rl-feedback-design.md §2.1/§4).
//
// Verbatim port of the per-negative target computation in upstream
// InterfaceRL.cpp:713-735 (memllib @ 0a541cc):
//
// pushStep = clamp(|avgRewardNeg|, 0.25, 1.0) * kGeometricPushScale
// dir[j] = neg_action[j] - meanPositiveAction[j]
// len = ||dir||
// useRandom = (len <= 1e-4) (disliked ON the centroid)
// effectivePushStep = pushStep / (1 + len) (taper for far items)
// d = useRandom ? random ∈ [-1,1] : dir[j] / len
// target[j] = clamp(neg_action[j] + d * effectivePushStep, 0, 1)
// inactive dims keep neg_action[j]
//
// SINGLE DELIBERATE FIRMWARE DIVERGENCE (recorded in ALIGNMENT.md): the
// upstream `useRandom` branch draws libc `rand() & 0xFF`; we draw from the
// caller's deterministic per-instance `nisps::Rng` so native == WASM parity
// holds. The branch only fires when a disliked action sits exactly on the
// centroid.
//
// Pure free functions over spans — no replay/centroid logic in the MLP
// kernel, no state, no heap (Anchor-First graft, ADR §0).
#pragma once
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <span>
#include "../core/perf.hpp"
#include "../core/rng.hpp"
namespace nisps::ml {
// Upstream InterfaceRL.hpp:293.
inline constexpr float kGeometricPushScale = 0.5f;
// pushStep from the mean negative reward (InterfaceRL.cpp:713).
inline float geo_push_step(float avg_reward_neg) noexcept {
float mag = (avg_reward_neg < 0.f) ? -avg_reward_neg : avg_reward_neg;
if (mag < 0.25f) mag = 0.25f;
if (mag > 1.0f) mag = 1.0f;
return mag * kGeometricPushScale;
}
// Compute the push-away target for ONE disliked action. `active_mask`
// (1 = active) gates which dims move — empty ⇒ all active (this is the solo/
// focus mask; upstream `activeDims_`). Writes n_out floats into `target`.
inline void compute_push_target(std::span<const float> neg_action,
std::span<const float> mean_positive,
std::span<const std::uint8_t> active_mask,
float push_step,
Rng& rng,
std::span<float> target) noexcept {
const std::size_t n = neg_action.size();
float len_sq = 0.f;
for (std::size_t j = 0; j < n; ++j) {
const float d = neg_action[j] - mean_positive[j];
len_sq += d * d;
}
const float len = std::sqrt(len_sq);
const bool use_random = (len <= 1e-4f);
const float effective = push_step / (1.0f + len);
for (std::size_t j = 0; j < n; ++j) {
const bool active =
active_mask.empty() || (j < active_mask.size() && active_mask[j] != 0u);
if (!active) {
target[j] = neg_action[j];
continue;
}
const float d = use_random
? rng.next_float_signed()
: ((neg_action[j] - mean_positive[j]) / len);
float t = neg_action[j] + d * effective;
if (t < 0.f) t = 0.f;
if (t > 1.f) t = 1.f;
target[j] = t;
}
}
// Dynamic LR ratio (InterfaceRL.cpp:742-743): push harder when dislikes are
// rare, gentler when they flood the buffer.
inline float geo_neg_lr_ratio(std::size_t neg_count, std::size_t pos_count) noexcept {
const std::size_t total = neg_count + pos_count;
const float neg_fraction = (total > 0u)
? static_cast<float>(neg_count) / static_cast<float>(total)
: 0.f;
return 0.5f - 0.4f * neg_fraction;
}
} // namespace nisps::ml