refactor(model): one canonical model class, enforced 3-tuple forward … - #49
Merged
chaudhryumer merged 1 commit intoSep 18, 2026
Conversation
…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.
|
@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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…contract
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.
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.
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:
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.
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.