petri_net_nn.onnx_export¶
onnx_export ¶
Export a trained :class:PetriNetModule to ONNX.
ONNX is the cross-runtime tensor-graph format. A PETRA module
exported here runs unchanged in any ONNX runtime — Python's
onnxruntime, C++, Java, browser (via onnxruntime-web), mobile,
and most accelerator stacks. That covers the
"deploy outside Python" half of the productionisation story
without forcing PETRA to ship platform-specific bindings.
The wrinkle PETRA introduces over an off-the-shelf
torch.onnx.export call is the dict-of-tensors input:
:meth:PetriNetModule.forward takes
input_marking={place_id: tensor} (and optionally
input_values={place_id: tensor}), not positional tensors.
ONNX's tracing model expects positional inputs with named axes.
The bridge:
- The caller declares a schema — the ordered list of place ids that will become positional inputs at inference time.
- We wrap the module in a small
nn.Modulewhose forward takes positional tensors, re-keys them into the dict shapes the underlying module expects, runs the forward pass, and returns a stack of the requested output transitions' activations. - We call
torch.onnx.exporton the wrapper.
What this lets you do: ship a trained PETRA model as an .onnx
file with a small JSON sidecar describing the schema (which input
tensor names map to which places). Consumers in any language can
load the ONNX file, pass tensors in the documented order, and get
out the per-transition activations.
Limitations carried forward:
- Time-unrolled mode with non-uniform transition durations uses
per-transition Python lists for the in-flight queue (see
:mod:
petri_net_nn.compiler). Those lists don't survive ONNX tracing. Acyclic mode (num_steps == 0) and time-unrolled mode where every transition has duration 1 export cleanly. - Softmax routing exports correctly. STE firing exports the forward path (the autograd detach trick is only relevant for training).
- Inhibitor arcs and structural-guard sigmoid gates export cleanly as standard tensor ops.
torch_guard/torch_output_valuecallables export iff their bodies use only ONNX-exportable torch ops (most do).
export_onnx ¶
export_onnx(module, output_path, *, input_places, input_value_places=None, output_transitions=None, batch_size=1, opset_version=17, dynamic_batch=True)
Export a trained module to ONNX.
Parameters¶
module
The trained :class:PetriNetModule to export. eval()
is called on it before tracing so any training-mode
randomness is disabled.
output_path
Where to write the .onnx file. Parent directory must
exist.
input_places
Ordered list of place ids whose activations are inputs at
inference time. The exported graph's first len(input_places)
positional tensors correspond to these places, in this
order. Place ids must exist on the module's net.
input_value_places
Optional ordered list of place ids whose values are
inputs (the coloured-Petri-net value channel). Omit for
pure marking-channel models. When supplied, the exported
graph's next positional tensors after the markings carry
these values.
output_transitions
Ordered list of transition ids whose firing activations
are returned. Defaults to all transitions whose label is
not an auto-generated gateway label (matches the
train_on_traces / anomaly_score convention).
batch_size
Batch size to use for the tracing example tensors. The
exported graph supports arbitrary batch sizes at runtime
when dynamic_batch=True (the default).
opset_version
ONNX opset to target. 17 is the modern baseline supported
by all currently-shipping ONNX runtimes.
dynamic_batch
When True (default), mark the batch dimension as
dynamic on every input and output. The exported graph
then accepts any batch size at inference time. Set to
False only for very space-constrained deployments
where shape inference is faster with a fixed batch.
Returns¶
dict[str, list[str]] A schema dict the caller can serialise (JSON, TOML, etc.) alongside the .onnx file. Keys:
* ``"input_marking_places"`` — names of the marking
inputs in the exported graph (``marking__<place_id>``);
* ``"input_value_places"`` — names of the value inputs;
* ``"output_transitions"`` — names of the output
transitions in the order they're stacked along the
last axis of the returned tensor.
Consumers in other languages use this schema to feed
tensors in the right order.
Source code in petri_net_nn/onnx_export.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |