ROOT
master
Reference Guide
Loading...
Searching...
No Matches
TMVA_SOFIE_PyTorch.py
Go to the documentation of this file.
1
### \file
2
### \ingroup tutorial_ml
3
### \notebook -nodraw
4
### This macro provides a simple example for the parsing of PyTorch .pt file
5
### into RModel object and further generating the .hxx header files for inference.
6
###
7
### \macro_code
8
### \macro_output
9
### \author Sanjiban Sengupta
10
11
import
sys
12
13
import
ROOT
14
import
torch
15
import
torch.nn
as
nn
16
17
SOFIE =
ROOT.TMVA.Experimental.SOFIE
18
19
# Python and C++ write to separate stdout buffers; flush both on every line so
20
# that the Python prints and the RModel printouts appear in order
21
sys.stdout.reconfigure
(line_buffering=
True
)
22
ROOT.gInterpreter.ProcessLine
(
"std::cout << std::unitbuf;"
)
23
24
# ------------------------------------------------------------------------------
25
# Step 1: Create, train and save a simple PyTorch model
26
# ------------------------------------------------------------------------------
27
28
model =
nn.Sequential
(
29
nn.Linear
(32, 16),
30
nn.ReLU
(),
31
nn.Linear
(16, 8),
32
nn.ReLU
(),
33
)
34
35
criterion =
nn.MSELoss
()
36
optimizer =
torch.optim.SGD
(
model.parameters
(), lr=0.01)
37
38
x =
torch.randn
(2, 32)
39
y =
torch.randn
(2, 8)
40
41
for
i
in
range
(500):
42
y_pred = model(x)
43
loss =
criterion
(y_pred, y)
44
optimizer.zero_grad
()
45
loss.backward
()
46
optimizer.step
()
47
48
model.eval
()
49
m =
torch.jit.script
(model)
50
torch.jit.save
(m,
"PyTorchModel.pt"
)
51
52
# ------------------------------------------------------------------------------
53
# Step 2: Parse the saved PyTorch .pt file with TMVA::SOFIE
54
# ------------------------------------------------------------------------------
55
56
# Parsing a PyTorch model requires the shape and data-type of input tensor
57
# Data-type of input tensor defaults to Float if not specified
58
input_shapes =
ROOT.std.vector
[
"std::vector<std::size_t>"
]()
59
input_shapes.push_back
([2, 32])
60
61
# Parsing the saved PyTorch .pt file into RModel object
62
model =
SOFIE.PyTorch.Parse
(
"PyTorchModel.pt"
, input_shapes)
63
64
# Generating inference code
65
model.Generate
()
66
model.OutputGenerated
(
"PyTorchModel.hxx"
)
67
68
# Printing required input tensors
69
print(
"\n"
)
70
model.PrintRequiredInputTensors
()
71
72
# Printing initialized tensors (weights)
73
print(
"\n"
)
74
model.PrintInitializedTensors
()
75
76
# Printing intermediate tensors
77
print(
"\n"
)
78
model.PrintIntermediateTensors
()
79
80
# Checking if tensor already exist in model
81
tensor_exists =
bool
(
model.CheckIfTensorAlreadyExist
(
"0weight"
))
82
print(f
'\n\nTensor "0weight" already exist: {str(tensor_exists).lower()}\n'
)
83
84
tensor_shape =
model.GetTensorShape
(
"0weight"
)
85
print(
'Shape of tensor "0weight": '
+
","
.join(str(dim)
for
dim
in
tensor_shape) +
","
)
86
87
tensor_type =
model.GetTensorType
(
"0weight"
)
88
print(f
'\nData type of tensor "0weight": {SOFIE.ConvertTypeToString(tensor_type)}'
)
89
90
# Printing generated inference code
91
print()
92
model.PrintGenerated
()
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:359
ROOT::Detail::TRangeCast
Definition
TCollection.h:312
bool
tutorials
machine_learning
TMVA_SOFIE_PyTorch.py
ROOTmaster - Reference Guide Generated on Tue Jul 14 2026 15:19:06 (GVA Time) using Doxygen 1.10.0