import inspect
import numpy as np
import ROOT
import torch
import torch.nn as nn
def CreateAndTrainModel(modelName):
model = nn.Sequential(nn.Linear(32, 16), nn.ReLU(), nn.Linear(16, 8), nn.ReLU(), nn.Linear(8, 2), nn.Softmax(dim=1))
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
for i in range(500):
x = torch.randn(2, 32)
y = torch.randn(2, 2)
y_pred = model(x)
loss = criterion(y_pred, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
model.eval()
modelFile = modelName + ".onnx"
dummy_x = torch.randn(1, 32)
model(dummy_x)
def filtered_kwargs(func, **candidate_kwargs):
sig = inspect.signature(func)
return {k: v for k, v in candidate_kwargs.items() if k in sig.parameters}
kwargs = filtered_kwargs(
torch.onnx.export,
input_names=["input"],
output_names=["output"],
external_data=False,
dynamo=True,
)
print("calling torch.onnx.export with parameters", kwargs)
torch.onnx.export(model, dummy_x, modelFile, **kwargs)
print("model exported to ONNX as", modelFile)
return modelFile
def ParseModel(modelFile, verbose=False):
parser = ROOT.TMVA.Experimental.SOFIE.RModelParser_ONNX()
model = parser.Parse(modelFile, verbose)
if verbose:
model.PrintInitializedTensors()
data = model.GetTensorData["float"]("0weight")
print("0weight", data)
data = model.GetTensorData["float"]("2weight")
print("2weight", data)
model.Generate()
model.OutputGenerated()
if verbose:
model.PrintGenerated()
modelCode = modelFile.replace(".onnx", ".hxx")
print("Generated model header file ", modelCode)
return modelCode
modelName = "LinearModel"
modelFile = CreateAndTrainModel(modelName)
modelCode = ParseModel(modelFile, False)
ROOT.gInterpreter.Declare('#include "' + modelCode + '"')
sofie = getattr(ROOT, "TMVA_SOFIE_" + modelName)
session = sofie.Session()
x = np.random.normal(0, 1, (1, 32)).astype(np.float32)
print("\n************************************************************")
print("Running inference with SOFIE ")
print("\ninput to model is ", x)
y = session.infer(x)
y_sofie = np.asarray(y.data())
print("-> output using SOFIE = ", y_sofie)
try:
import onnxruntime as ort
print("Running inference with ONNXRuntime ")
ort_session = ort.InferenceSession(modelFile)
outputs = ort_session.run(None, {"input": x})
y_ort = outputs[0]
print("-> output using ORT =", y_ort)
testFailed = abs(y_sofie - y_ort) > 0.01
if np.any(testFailed):
raise RuntimeError("Result is different between SOFIE and ONNXRT")
else:
print("OK")
except ImportError:
print("Missing ONNXRuntime: skipping comparison test")
calling torch.onnx.export with parameters {'input_names': ['input'], 'output_names': ['output'], 'external_data': False, 'dynamo': True}
[torch.onnx] Obtain model graph for `Sequential([...]` with `torch.export.export(..., strict=False)`...
[torch.onnx] Obtain model graph for `Sequential([...]` with `torch.export.export(..., strict=False)`... ✅
[torch.onnx] Run decompositions...
[torch.onnx] Run decompositions... ✅
[torch.onnx] Translate the graph into ONNX...
[torch.onnx] Translate the graph into ONNX... ✅
[torch.onnx] Optimize the ONNX graph...
[torch.onnx] Optimize the ONNX graph... ✅
model exported to ONNX as LinearModel.onnx
Generated model header file LinearModel.hxx
************************************************************
Running inference with SOFIE
input to model is [[-0.44226584 1.3856065 -0.43362024 1.0033153 0.81393427 -0.10947526
1.2027806 -2.0051267 1.0863305 0.59330493 2.1961653 0.41707766
-0.7613932 0.11439737 -1.046573 -0.5104994 -0.31258753 -1.5730503
-0.58353907 0.14983174 0.9839207 0.09004886 -0.6810219 -0.33765307
-1.2893789 0.5972082 -1.008434 -0.5272349 2.746534 0.9780353
-1.9684738 -0.4676 ]]
-> output using SOFIE = [0.54799724 0.45200276]
Missing ONNXRuntime: skipping comparison test