Skip to content

nav: the belief net — .cvcnm version 2, C-channel risk patches, the patch crop and the gate in training (B3) - #746

Merged
transfix merged 8 commits into
masterfrom
feat/nav-belief-material-net
Oct 20, 2026
Merged

transfix merged 8 commits into
masterfrom
feat/nav-belief-material-net

Conversation

@transfix

@transfix transfix commented Oct 20, 2026 •

Copy link
Copy Markdown
Owner

This is B3 of docs/roadmap/NAV-BELIEF-MATERIAL-ROADMAP.md: the belief net. It adds a 3-channel [r~, hard, seen] risk patch, .cvcnm version 2, the crop that builds that patch from belief planes, and the witness gate in training. It follows §9's recommended column, and everything is additive: a 2-channel net and an ungated batch behave exactly as before.

On top of #734, merged as f7930c0. master is merged in (376e91a), so the diff is B3's change alone, and the full CI/CD runs on it.

.cvcnm

  • Version 2 is for a 3-channel net. It is the version-1 layout plus a belief block after eps, <IIfIIff>: risk_channels, seen_rule, unknown_risk, unknown_hard, unknown_gate_hard, material_range_m, patch_extent_m. That is the layout GRL-SNAM's matnet_export already writes (belief material (T2, part 1): a 3-channel net, .cvcnm v2, the gate in training CVC-Lab/GRL-SNAM#129).
  • A 2-channel net is still written as version 1, byte for byte. libcvc 3.7.0 and earlier refuse a version-2 file instead of reading its encoder with 2-channel strides.
  • The loader now:
    • bounds-checks every read;
    • checks every known tensor's shape against the hyperparams;
    • checks the encoder's input channels against the header;
    • keeps the meta trailer, which serialize() now writes back (it used to drop it).
  • New accessors: risk_channels(), belief_header(), set_belief_header(), meta().

The net and the trainer

  • The CPU and CUDA forward and backward and the trainer take C = risk_channels() channels; material_batch::risk_patch is [B*C*P*P].
  • material_batch::gate_active ([B] u8, null = ungated): each agent's lam_soft enters every rollout times its bit, and so does its gradient through the rollout. L_lreg reads the head's own value, matching the torch trainer.
  • material_patch_belief (belief_material.h) is the model's patch for one agent. It takes P×P samples over extent_m: r~ bilinear, hard and seen from the nearest cell, and the pinned values where that cell is unseen or off the map. It is BIT, with the op order pinned in the header.
  • pycvc:
    • nav_material_patch_belief;
    • nav_material_trainer_risk_channels;
    • a trailing gate_active on nav_material_trainer_step / _loss;
    • (B,C,P,P) patches in nav_matnet_forward and the trainer.

Tests

check measured
.cvcnm version 1 with meta round-trips byte for byte
version 2 the block at its offsets; round-trips
the loader refuses a 3-channel version 1, an unknown version, a bad block, a mismatched d_tok and truncation
a zero seen channel the 2-channel net, forward and backward, exactly
the crop cell centres, off the map, unseen and between cells; pycvc equals a numpy twin byte for byte
full-chain gradcheck, belief net + gate dir_rel 3.8e-5 (surrogate), 4.4e-4 (bicycle)
gate all on the ungated loss and gradients, byte for byte
CUDA vs CPU, 3 channels (GTX 1650) forward 3e-7, backward 1.2e-4 (cosine 1.000000)
the 2-channel small trainer golden byte-identical to the committed one
existing suites nav_coef_energy (4), nav_coef_energy_grad (6), nav_material_optim (3), nav_material_train (16), the CUDA suites (4 + 4), test_pycvc_nav_train

Golden 3 is NavMaterialTrain.TorchTrainerGoldenBelief, written with CVC_MATERIAL_TRAIN_GOLDEN_DIR, like the small golden. It uses:

  • the small golden's widths, with 3 channels;
  • patches cropped by material_patch_belief from golden 1's full planes (the JSON records the crop inputs);
  • the gate on agents 0 and 2.

Its _w0 / _w3 files are .cvcnm version 2. The loss goes 49.43 → 41.69 over 3 steps. A numpy twin of the crop reproduces its four patches bit for bit; they are 92%, 100%, 0% and 51% seen.

The deployed drive steers the material policy and an external force together
(drive_step_material_ext with an ext_force: cvcdbg's RF/comm force), but the material trainer
trained the material heads with no such force present. material_ext (material.h) puts it in every
material training rollout, joined as the deployed drive joins it:
- the surrogate adds it to ftot (a point mass: steer has nothing to act on);
- the bicycle and the dynamic vehicle add it last to F = F_bar + F_goal + F_mat, and with steer to
  the steering bias (F_rep + F_mat + F_ext) . left;
- a null channel is the rollout without one, bit for bit (conditional adds).

Sources, exactly one: K force patches per agent on the rollout patch's grid with a per-step
schedule (a force that depends only on the agent's position, e.g. fixed radios and jammers
switching on and off; J is the patch's gradient), per-step values with an optional J, or a C++
callback with hold. The VJPs chain J^T dL/dF_ext into the position adjoint (a held window's sum at
its sampled step) and return dL/dF_ext per step. detail/material_ext.h holds the per-step sampler
and the reverse's window. material_batch::ext carries it through material_loss /
material_loss_and_grad; L_multi leaves it out; with it the surrogate runs on the CPU.

pycvc: optional trailing ext_patch / ext_patch_of_step / ext_F / ext_J / ext_steer on
nav_integrate_surrogate_material, nav_integrate_bicycle_material(_vjp) (want_g_fext returns g_fext)
and nav_material_trainer_step / _loss, checked with the GIL held.

Tests: nav_material_ext_test (null and zero force bit-identical on all three integrators; the
coefficient gradients through a scheduled force patch and through a held callback against finite
differences; dL/dF_ext; a callback giving the patch is the patch; steer; malformed channels;
NavMaterialExt.Golden writes tests/data/material_ext_golden.json for GRL-SNAM's torch twins),
nav_material_train_test's full chain with an external force (surrogate and bicycle), and
test_pycvc_nav_train's test_external_force (the bindings reproduce the golden to 4e-9).

Also: sim_world.h's set_ext_force comment said the ext force had no effect on the material path;
step() fuses them through drive_step_material_ext.
…r a torch twin trainer

material_loss and material_loss_and_grad take an optional material_loss_parts: L, each unweighted
term (traj, vel, fric, clear, nav, lreg, multi) and the CVaR eta, for a twin trainer's parity and
for logging. TheLossPartsAddUpToTheLoss checks they sum to L on both entry points.

TorchTrainerGolden runs the native trainer for 3 Adam steps on the fixture batch with a scheduled
external force patch, and with CVC_MATERIAL_TRAIN_GOLDEN_DIR set writes the golden GRL-SNAM's torch
material trainer is held to: the config, batch and force (JSON), the weights before and after
(.cvcnm), step 0's weight gradients (float32) and each step's loss terms. Not committed (2.5 MB).
Comments and docs only: ext_force is a hook for a caller's own force, and the golden is for a torch twin of the loss; no downstream user is named.
…ittable downstream

build_test_model takes the header's widths (d_tok, nhead, num_layers, d_risk); the table at the default widths is the fixed one (TestModelTableAtDefaultWidths), so every existing test and golden is unchanged. The golden test is now a helper: TorchTrainerGolden as before (loss 42.952275 -> 25.870981, eta 33.5428), and TorchTrainerGoldenSmall at d_tok 16, 2 heads, 1 layer, d_risk 16 on a batch of 4 with a 9x9 rollout patch: 59,542 parameters, about 0.8 MB written, small enough for a torch twin's CI to commit.
# Conflicts:
#	docs/NAV_MATERIAL.md
…atch crop and the gate in training (B3)

B3 of docs/roadmap/NAV-BELIEF-MATERIAL-ROADMAP.md, with §9's recommended column:
- .cvcnm version 2 for a 3-channel net ([r~, hard, seen]): the version-1 layout plus a
  belief block after eps, <IIfIIff> (risk_channels, seen_rule, unknown_risk,
  unknown_hard, unknown_gate_hard, material_range_m, patch_extent_m), the layout
  GRL-SNAM's matnet_export writes. A 2-channel net is still version 1, byte for byte.
  coef_energy_net::risk_channels() / belief_header() / set_belief_header() / meta().
- The loader checks every known tensor's shape against the hyperparams and the risk
  encoder's channels against the header, bounds-checks every read, and keeps the meta
  trailer, which serialize() now writes back (it used to drop it).
- The CPU and CUDA forward and backward and the trainer take C = risk_channels()
  channels: material_batch::risk_patch is [B*C*P*P].
- material_batch::gate_active ([B] u8, null = ungated): each agent's lam_soft enters
  every rollout times its bit, and its gradient through the rollout is gated the same
  way; L_lreg reads the head's own lam_soft (as GRL-SNAM's torch trainer does).
- material_patch_belief (belief_material.h): the model's 3-channel patch, cropped from
  belief planes around an agent; BIT, op order pinned in the header.
- pycvc: nav_material_patch_belief; nav_material_trainer_risk_channels; a trailing
  gate_active on nav_material_trainer_step / _loss; nav_matnet_forward and the trainer
  take (B,C,P,P) patches.

Tests:
- nav_belief_material_net_test: version 1 round-trips byte for byte with its meta;
  version 2 carries the block at the right offsets and round-trips; the loader refuses
  a 3-channel version 1, an unknown version, a bad block, a mismatched d_tok and
  truncation; a zero seen channel is the 2-channel net in the forward and backward; the
  crop at cell centres, off the map, unseen, and between cells.
- nav_material_train_test: the full-chain gradcheck on the belief net with the gate
  (surrogate dir_rel 3.8e-5, bicycle 4.4e-4); the gate on is ungated byte for byte;
  TorchTrainerGoldenBelief (golden 3: the small golden's widths with 3 channels,
  patches cropped from golden 1, the gate on agents 0 and 2; loss 49.43 -> 41.69).
  The 2-channel small golden is byte-identical to before.
- nav_coef_energy_cuda_test: the 3-channel forward (3e-7) and backward (1.2e-4, cos 1)
  match the CPU on a GTX 1650.
- test_pycvc_nav_train: the belief net trains with the gate and saves as version 2;
  the crop matches its numpy twin byte for byte.
@transfix
transfix changed the base branch from feat/material-ext to master October 20, 2026 17:38
…erial-net

# Conflicts:
#	bindings/pycvc/pycvc_nav.i
#	bindings/pycvc/test_pycvc_nav_train.py
#	inc/cvc/nav/material_train.h
#	src/cvc/nav/material_train.cpp
#	src/cvc/tests/coef_energy_test_model.h
#	src/cvc/tests/nav_material_train_test.cpp
transfix added a commit to CVC-Lab/GRL-SNAM that referenced this pull request Oct 20, 2026
…r both trainers (#131)

The torch side of libcvc's B3 (transfix/libcvc#746) for belief (observed) material:
- grl_snam/belief_material.py: material_patch_belief, the model's 3-channel
  [r~, hard, seen] patch cropped from belief planes around an agent (r~ bilinear, hard
  and seen from the nearest cell, the pinned values where it is unseen or off the map),
  bit-identical to libcvc's kernel.
- material_train_native.MaterialTrainer passes a batch's gate_active to the native
  trainer (HAS_MATERIAL_TRAINER_GATE), and reports risk_channels.
- tests/fixtures/material_train_golden_belief: libcvc's TorchTrainerGoldenBelief (the
  small golden's widths with the 3-channel net, patches cropped from golden 1, the gate
  on agents 0 and 2; .cvcnm version 2 weights).

Tests:
- test_material_train_torch: the belief golden in the step-0, three-Adam-step and
  terms tests (CPU, float32 and float64, the small golden's tolerances), and a live
  torch-vs-native comparison of the belief net with the gate on the surrogate and the
  bicycle.
- test_belief_material_parity: the crop reproduces the golden's four patches byte for
  byte, pins unseen and off-map samples, and matches pycvc's nav_material_patch_belief.
@transfix
transfix merged commit 671d358 into master Oct 20, 2026
20 of 24 checks passed
transfix added a commit that referenced this pull request Oct 21, 2026
…emented (#755)

The status line of docs/roadmap/NAV-BELIEF-MATERIAL-ROADMAP.md still said B3 follows. B1 (#742), B2 (#744) and B3 (#746) have all merged, built with section 9's recommended column.
@transfix
transfix deleted the feat/nav-belief-material-net branch October 29, 2026 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant