Skip to content

Conversation

@pobonomo
Copy link
Member

This is kind a lot of things that needs testing the goal is to get as many vnn competition models understood.

Needs ONNX, new types of layer (conv2D, pooling,...), new network architectures (skip connections,...), and parsing vnnlib files

pobonomo and others added 18 commits October 5, 2025 10:17
Started doing a convolutional net with keras.

notebooks/adversarial/adversarial_cnn.ipynb

contains a working example that we would want to model with gurobi_ml

A conv2d layer is in progress (doesn't crash and does something).

The big mess now is variable dimensions. Gurobi ML was more or less
designed around the idea that the input of the ML model is 2-d
(n_examples, n_features) but of course convolutional layers are 4-d
(n_examples, height, width, n_channels)
The 2d assumption is all other the place...

Missing layers:
- MaxPooling
- Dropout
- Flatten

Also for the conv2d only did with valid padding and the for loop is
Let's see if chat gpt can do the max pooling and flatten layers
We get something unsolvable on my example :-()
A very tiny one without pooling layers.
A less tiny with pooling. Right now I need to cheat on the bounds
of the input layer.
Add MatMult and Plus
This commit adds full support for neural networks with arbitrary Directed
Acyclic Graph (DAG) topologies, including skip connections, residual
connections, and multi-branch architectures.

New Features:
- DAGNNConstr: Base class for neural networks with DAG topology
- AddLayer: New layer type for element-wise addition (residual connections)
- ONNXDAGNetworkConstr: ONNX implementation with topological sorting
- add_onnx_dag_constr: New API function for DAG models

Technical Implementation:
- Kahn's algorithm for topological sorting of computation graphs
- Tensor variable tracking through the graph
- Support for tensor reuse (skip/residual connections)
- Handles unnamed ONNX nodes correctly

Supported ONNX Operations:
- Gemm (fully connected layers with transB support)
- MatMul (matrix multiplication)
- Add (both bias addition and residual connections)
- Relu (activation)
- Identity (pass-through)

Architecture Support:
 Sequential networks (backward compatible)
 Skip connections (input used by multiple layers)
 Residual connections (intermediate outputs reused)
 Multi-branch architectures

Testing:
- 4 new comprehensive tests in test_onnx_dag.py
- All existing tests continue to pass (backward compatible)
- Validated with real CerSyVe models (pendulum, cart_pole, etc.)
- Accuracy: < 1e-7 error vs ONNX Runtime

Backward Compatibility:
- Original add_onnx_constr and BaseNNConstr unchanged
- New functionality is opt-in via add_onnx_dag_constr
- Existing code continues to work without modification
Replace the custom implementation of Kahn's algorithm with Python's
standard library graphlib.TopologicalSorter (available since Python 3.9).

Benefits:
- More maintainable (uses standard library)
- Better tested (Python standard library)
- Cleaner code (less custom implementation)
- Better error messages (CycleError with details)

The project requires Python 3.10+, so graphlib is always available.

Changes:
- Import graphlib module
- Replace manual Kahn's algorithm with TopologicalSorter
- Use static_order() to get sorted nodes
- Catch graphlib.CycleError for cycle detection

All tests continue to pass with identical behavior.
Update Keras and PyTorch documentation to guide users on using ONNX
export for models with skip connections and residual architectures.

Changes to Keras (keras.py):
- Added comprehensive 'Models with Skip Connections' section
- Provided two export methods: tf2onnx and keras2onnx
- Included complete code examples
- Linked to tf2onnx, keras2onnx, and ONNX documentation

Changes to PyTorch (sequential.py):
- Added 'Models with Skip Connections' section
- Provided torch.onnx.export example with all parameters
- Included verification steps
- Linked to PyTorch ONNX tutorials and documentation

Changes to ONNX module (__init__.py):
- Enhanced module docstring with usage examples
- Added quick reference for Keras and PyTorch export
- Clarified difference between add_onnx_constr and add_onnx_dag_constr

New file: ONNX_EXPORT_GUIDE.md
- Complete guide for using DAG models via ONNX
- Step-by-step examples for Keras and PyTorch
- ResNet and skip connection examples
- Troubleshooting section
- Installation requirements
- Links to external documentation

This addresses the workflow where users with complex neural network
architectures (ResNet, skip connections, etc.) should export to ONNX
first rather than using the direct Keras/PyTorch APIs which only
support sequential models.

References included:
- PyTorch ONNX tutorials: pytorch.org/tutorials
- tf2onnx documentation: github.com/onnx/tensorflow-onnx
- keras2onnx documentation: github.com/onnx/keras-onnx
- ONNX official docs: onnx.ai
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Pierre Bonami seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Pierre Bonami seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

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