Skip to content

refactor(model): one canonical model class, enforced 3-tuple forward … - #49

Merged
chaudhryumer merged 1 commit into
QuantumLogicsLabs:mainfrom
mustfaaaa:model-interface-consolidation
Sep 18, 2026
Merged

chaudhryumer merged 1 commit into
QuantumLogicsLabs:mainfrom
mustfaaaa:model-interface-consolidation

Conversation

@mustfaaaa

Copy link
Copy Markdown
Contributor

…contract

  1. Remove the silent LSTM fallback solver_model.py wrapped its model import in try/except and, on ImportError, silently defined an entirely different LSTM-based CalculusSolverModel, so a broken model/transformer.py trained the wrong network with no error. It is now a hard-import re-export; an unimportable model file fails at import time.

  2. Consolidate to one tree-based model class model/transformer.py::CalculusSolverModel is canonical: it is what train.py trains, what checkpoints/final/best.pt is a state dict of, and the only class whose forward(src, tgt) matches how beam_search calls it. model/architecture.py::CalculusModel is retired. Beyond the constructor difference (rule_labels= vs num_rules=) its forward() took four positional arguments, so beam_search could only run it via a TypeError-retry shim. Removed its import and load branch from inference/solve.py; tree-keyed checkpoints now load into the canonical class.

  3. Document and enforce the 3-tuple return contract Documented at CalculusSolverModel.forward(). Added check_forward_contract(), which train.py now runs before the first training step (~60 ms at the real config). Call-site audit:

  • train.py (4 sites), predict.py: already unpacked explicitly.
  • inference/beam_search.py: two isinstance(output, tuple) guards replaced with explicit unpacking through _forward(), which raises a clear TypeError on a contract violation.
  • inference/solve.py: PklTransformerModel (legacy model/model.pkl loader) returned a single tensor. It now accepts the canonical signature and returns (logits, None, None); it has no rule or verifier head.
  • deployment/: uses the ONNX session's named "logits" output, not a torch forward(), so is unaffected.
  1. Reconcile beam_search() signature drift No caller passes src_positions / parent_child_pairs; removed from beam_search(). The _call_model helper that caught TypeError and retried with alternative argument shapes is gone -- it masked genuine TypeErrors raised inside forward() as signature mismatches. test_prefix_parity.py's 4-arg-compatibility test is inverted to assert that a non-canonical model now fails loudly.

  2. Interface smoke test tests/unit/test_model_interface.py: instantiates the model, runs a dummy forward pass, asserts the 3-tuple and shapes; covers teacher-forced rule ids, the contract checker rejecting single-tensor and mis-shaped outputs, no fallback path in solver_model.py (AST check), the retired module being gone, the legacy pkl model conforming, and beam_search end to end against a real CalculusSolverModel.

Tests: 246 -> 259 passing, 4 xfailed.

Not changed, flagged: model/simple_transformer.py::SimpleCalculusModel (ONNX export) still returns a single tensor. Changing it alters the exported graph's outputs that deployment/onnx_beam_search.py reads by name, so it is left for the ONNX path's owner.

…contract

1. Remove the silent LSTM fallback
solver_model.py wrapped its model import in try/except and, on ImportError,
silently defined an entirely different LSTM-based CalculusSolverModel, so a
broken model/transformer.py trained the wrong network with no error. It is now
a hard-import re-export; an unimportable model file fails at import time.

2. Consolidate to one tree-based model class
model/transformer.py::CalculusSolverModel is canonical: it is what train.py
trains, what checkpoints/final/best.pt is a state dict of, and the only class
whose forward(src, tgt) matches how beam_search calls it.
model/architecture.py::CalculusModel is retired. Beyond the constructor
difference (rule_labels= vs num_rules=) its forward() took four positional
arguments, so beam_search could only run it via a TypeError-retry shim.
Removed its import and load branch from inference/solve.py; tree-keyed
checkpoints now load into the canonical class.

3. Document and enforce the 3-tuple return contract
Documented at CalculusSolverModel.forward(). Added check_forward_contract(),
which train.py now runs before the first training step (~60 ms at the real
config). Call-site audit:
- train.py (4 sites), predict.py: already unpacked explicitly.
- inference/beam_search.py: two isinstance(output, tuple) guards replaced
  with explicit unpacking through _forward(), which raises a clear TypeError
  on a contract violation.
- inference/solve.py: PklTransformerModel (legacy model/model.pkl loader)
  returned a single tensor. It now accepts the canonical signature and
  returns (logits, None, None); it has no rule or verifier head.
- deployment/: uses the ONNX session's named "logits" output, not a torch
  forward(), so is unaffected.

4. Reconcile beam_search() signature drift
No caller passes src_positions / parent_child_pairs; removed from
beam_search(). The _call_model helper that caught TypeError and retried with
alternative argument shapes is gone -- it masked genuine TypeErrors raised
inside forward() as signature mismatches. test_prefix_parity.py's
4-arg-compatibility test is inverted to assert that a non-canonical model
now fails loudly.

5. Interface smoke test
tests/unit/test_model_interface.py: instantiates the model, runs a dummy
forward pass, asserts the 3-tuple and shapes; covers teacher-forced rule
ids, the contract checker rejecting single-tensor and mis-shaped outputs,
no fallback path in solver_model.py (AST check), the retired module being
gone, the legacy pkl model conforming, and beam_search end to end against a
real CalculusSolverModel.

Tests: 246 -> 259 passing, 4 xfailed.

Not changed, flagged: model/simple_transformer.py::SimpleCalculusModel (ONNX
export) still returns a single tensor. Changing it alters the exported
graph's outputs that deployment/onnx_beam_search.py reads by name, so it is
left for the ONNX path's owner.
@vercel

vercel Bot commented Sep 18, 2026

Copy link
Copy Markdown

@mustfaaaa is attempting to deploy a commit to the seno-quantum-coder's projects Team on Vercel.

A member of the Team first needs to authorize it.

@chaudhryumer
chaudhryumer merged commit 86dea87 into QuantumLogicsLabs:main Sep 18, 2026
4 of 5 checks passed
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.

2 participants