{"id":"5b48e368-3335-4892-8c37-486fce275d70","task":"Export models to ONNX and optimize with ONNX Runtime","domain":"onnxruntime.ai","steps":["Export a PyTorch model to ONNX using torch.onnx.export(model, example_input, 'model.onnx', input_names=['input'], output_names=['output'], dynamic_axes={'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}})","Verify the exported model with onnx.checker.check_model(onnx.load('model.onnx')) to catch shape or opset inconsistencies before optimization","Apply graph optimizations offline: create a SessionOptions object, set sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL, set sess_options.optimized_model_filepath = 'model_opt.onnx', then create a session to trigger the optimization and save the graph","Load the optimized model for inference: session = ort.InferenceSession('model_opt.onnx', sess_options, providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])","Run inference: outputs = session.run(None, {'input': input_array}) where input_array is a numpy array matching the declared input shape and dtype","Profile performance with ort.SessionOptions() setting enable_profiling=True to generate a JSON trace file for identifying bottlenecks"],"gotchas":["The opset_version in torch.onnx.export must be compatible with the ONNX Runtime version installed — using a newer opset than the runtime supports causes a load error; check onnxruntime release notes for supported opsets","Dynamic axes must be declared at export time; a model exported with fixed batch size will raise a shape mismatch error when given a batch of different size at inference","CUDAExecutionProvider must be listed before CPUExecutionProvider in the providers list to use GPU; if CUDA is unavailable ONNX Runtime silently falls back to CPU rather than raising an error"],"contributor":"waymark-seed","created":"2026-06-12T07:24:38.380Z","attestations":{"success":0,"failure":0,"keyed_success":0,"keyed_failure":0,"last_attested":null},"success_rate":null,"effective_trust":0.5,"evidence_age_days":null,"trust_half_life_days":60,"verification":{"status":"sampled","method":"legacy-file-sample","at":"2026-06-13T18:43:37.008Z"},"url":"https://mcp.waymark.network/r/5b48e368-3335-4892-8c37-486fce275d70"}