Skip to content

feat(kwyk): import published TF kwyk weights with a verified mapping - #384

Open
dhritimandas wants to merge 1 commit into
neuronets:alphafrom
dhritimandas:feat/kwyk-weight-conversion
Open

feat(kwyk): import published TF kwyk weights with a verified mapping#384
dhritimandas wants to merge 1 commit into
neuronets:alphafrom
dhritimandas:feat/kwyk-weight-conversion

Conversation

@dhritimandas

@dhritimandas dhritimandas commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Summary

What: We add a converter for the published kwyk TensorFlow weights. It loads them into the PyTorch KWYKMeshNet. We verify the mapping against the real container and the live TF graph.
Why: The trusted weights exist only inside the neuronets/kwyk Docker container. Retraining from scratch does not reproduce them. Verification also found three model defects. We fix them here.

Converter (nobrainer/datasets/convert_kwyk.py)

  • --npz (pre-extracted, no TF needed) or --tf-path (requires tensorflow)
  • load_state_dict(strict=True) as a correctness gate
  • offline parity check over all 8 conv layers, classifier included
  • <out>.provenance.json sidecar: source path + SHA-256 + arch args (MAP and BD checkpoints are structurally identical — the hash is the only durable record of which model a .pth came from)
  • biases imported by default; --drop-bias is an explicit, logged opt-out (dropping shifts logits by max ~20 vs ~1e-4)
  • layer-index base (0/1) detected and validated, never assumed

Model fixes exposed by verification

  • classifier is now FFGConv3d, not nn.Conv3d — the TF logits/ layer is a full VWN conv; the plain conv discarded its sigmas and contributed 0 MC variance (TF original: ~11.5). All five logits/conv3d/* vars now map 1:1
  • from-scratch training consequence: the output layer now joins kl_divergence() and samples under mc_vwn=True (more faithful to the paper, but a training-objective change)
  • ConcreteDropout3d clamp widened [0.05, 0.95] -> [0.01, 0.99] in model + converter together; old ceiling silently clipped 598/672 (89%) of the published SSD p values (max 0.954055). Unit test pins the two ranges to each other
  • bias flag threaded through hidden layers (default False, unchanged)

Verification

  • variable inventory vs real container: 41/41/48 vars, logits/conv3d/* namespace (not layer_8/), layer_1..7 1-based, biases 24/24
  • converted MAP vs live TF graph: max|diff| = 9.5e-05 (rel ~4e-6), 100% argmax agreement
  • converted SSD: all 672 p preserved to 6e-08; deterministic path bit-reproducible; MC variance ~10
  • full report incl. six discrepancies found/fixed + independent re-verification: docs/kwyk_mapping_verification.md
  • converted weights deliberately not committed (no weights in git); container + converter reproduce them deterministically

Test plan

The kwyk architecture (McClure et al. 2019) was reimplemented in PyTorch,
but users could not obtain the trusted published weights -- the three
SavedModels ship only inside the neuronets/kwyk Docker container, and no
conversion path existed.

- nobrainer/datasets/convert_kwyk.py: TF SavedModel/.npz -> KWYKMeshNet
  state_dict converter. Strict load, offline parity check over all 8 conv
  layers (including the classifier), and a .provenance.json sidecar
  recording the source path + SHA-256 -- the MAP and BD checkpoints are
  structurally indistinguishable, so the content hash is the only durable
  record of which model a .pth came from.
- KWYKMeshNet: the output classifier is now an FFGConv3d (was plain
  nn.Conv3d), matching the TF logits/ layer, which is itself a full VWN
  conv. All five logits variables map 1:1 with no information loss; the
  previous mean-path collapse made MC uncertainty under-dispersed at the
  output (TF contributes ~11.5 of run-to-run logit variance there; the
  plain conv contributed 0). Also adds a bias flag threaded through the
  hidden layers (default False, unchanged).
- ConcreteDropout3d clamp widened [0.05, 0.95] -> [0.01, 0.99] in lockstep
  with the converter: the published SSD checkpoint stores p up to
  0.954055, so the old ceiling silently clipped 89% of trained values
  (598/672). A unit test pins the two clamp ranges together.
- Biases are imported by default (--drop-bias is an explicit, logged
  opt-out): every published checkpoint carries bias_m/bias_a for every
  conv layer, and dropping them shifts output logits by max ~20.

Every mapping claim is verified against the real container and validated
numerically against the live TF graph: the converted MAP model reproduces
the original's logits to max|diff| = 9.5e-05 (relative error ~4e-6) on
identical input; converted SSD preserves all 672 dropout probabilities to
6e-08 and reproduces output-layer MC variance. Full verification report,
including the six discrepancies found and fixed and a second independent
re-verification: docs/kwyk_mapping_verification.md.
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