forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestRModelParserPyTorch.C
More file actions
169 lines (138 loc) · 7.31 KB
/
Copy pathTestRModelParserPyTorch.C
File metadata and controls
169 lines (138 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
#include <numeric>
#include "TMVA/RSofieReader.hxx"
#include <Python.h>
#include "TSystem.h"
#include "gtest/gtest.h"
#include <cmath>
constexpr float DEFAULT_TOLERANCE = 1e-6f;
void GenerateModels() {
FILE* fPyTorchModels;
fPyTorchModels = fopen("generatePyTorchModels.py", "r");
PyRun_SimpleFile(fPyTorchModels, "generatePyTorchModels.py");
std::cout << "PyTorch models are generated\n";
}
TEST(RModelParser_PyTorch, SEQUENTIAL_MODEL)
{
constexpr float TOLERANCE = DEFAULT_TOLERANCE;
std::vector<float> inputSequential ={-1.6207, 0.6133,
0.5058, -1.2560,
-0.7750, -1.6701,
0.8171, -0.2858};
// Generating PyTorch models for testing
Py_Initialize();
if (gSystem->AccessPathName("PyTorchModelSequential.pt",kFileExists))
GenerateModels();
// use RSofieReader to parse and evaluate the model
// need first the input shape
std::vector<size_t> inputTensorShapeSequential{2,4};
std::vector<std::vector<size_t>> inputShapesSequential{inputTensorShapeSequential};
TMVA::Experimental::RSofieReader s("PyTorchModelSequential.pt", inputShapesSequential);
std::vector<float> outputSequential = s.Compute(inputSequential);
PyObject* main = PyImport_AddModule("__main__");
PyObject* fGlobalNS = PyModule_GetDict(main);
PyObject* fLocalNS = PyDict_New();
if (!fGlobalNS) {
throw std::runtime_error("Can't init global namespace for Python");
}
if (!fLocalNS) {
throw std::runtime_error("Can't init local namespace for Python");
}
PyRun_String("import torch",Py_single_input,fGlobalNS,fLocalNS);
PyRun_String("model=torch.jit.load('PyTorchModelSequential.pt')",Py_single_input,fGlobalNS,fLocalNS);
PyRun_String("input=torch.reshape(torch.FloatTensor([-1.6207, 0.6133,"
" 0.5058, -1.2560,"
"-0.7750, -1.6701,"
" 0.8171, -0.2858]),(2,4))",Py_single_input,fGlobalNS,fLocalNS);
PyRun_String("output=model(input).detach().numpy().reshape(2,6)",Py_single_input,fGlobalNS,fLocalNS);
PyRun_String("outputFlat=output.flatten().tolist()",Py_single_input,fGlobalNS,fLocalNS);
PyObject* pSequentialValues=PyDict_GetItemString(fLocalNS,"outputFlat");
std::size_t pOutputSequentialSize=(std::size_t)PyList_Size(pSequentialValues);
//Testing the actual and expected output tensor sizes
EXPECT_EQ(outputSequential.size(), pOutputSequentialSize);
//Testing the actual and expected output tensor values
for (size_t i = 0; i < outputSequential.size(); ++i) {
float pOutputSequential=(float)PyFloat_AsDouble(PyList_GetItem(pSequentialValues, i));
EXPECT_LE(std::abs(outputSequential[i] - pOutputSequential), TOLERANCE);
}
}
TEST(RModelParser_PyTorch, MODULE_MODEL)
{
constexpr float TOLERANCE = DEFAULT_TOLERANCE;
std::vector<float> inputModule={0.5516, 0.3585,
-0.4854, -1.3884,
0.8057, -0.9449,
0.5626, -0.6466,
-1.8818, 0.4736,
1.1102, 1.8694};
Py_Initialize();
if (gSystem->AccessPathName("PyTorchModelModule.pt",kFileExists))
GenerateModels();
std::vector<size_t> inputTensorShapeModule{2,6};
std::vector<std::vector<size_t>> inputShapesModule{inputTensorShapeModule};
TMVA::Experimental::RSofieReader s("PyTorchModelModule.pt", inputShapesModule);
std::vector<float> outputModule = s.Compute(inputModule);
PyObject* main = PyImport_AddModule("__main__");
PyObject* fGlobalNS = PyModule_GetDict(main);
PyObject* fLocalNS = PyDict_New();
if (!fGlobalNS) {
throw std::runtime_error("Can't init global namespace for Python");
}
if (!fLocalNS) {
throw std::runtime_error("Can't init local namespace for Python");
}
PyRun_String("import torch",Py_single_input,fGlobalNS,fLocalNS);
PyRun_String("model=torch.jit.load('PyTorchModelModule.pt')",Py_single_input,fGlobalNS,fLocalNS);
PyRun_String("input=torch.reshape(torch.FloatTensor([0.5516, 0.3585, "
"-0.4854, -1.3884,"
" 0.8057, -0.9449,"
" 0.5626, -0.6466,"
"-1.8818, 0.4736,"
" 1.1102, 1.8694]),(2,6))",Py_single_input,fGlobalNS,fLocalNS);
PyRun_String("output=model(input).detach().numpy().reshape(2,12)",Py_single_input,fGlobalNS,fLocalNS);
PyRun_String("outputFlat=output.flatten().tolist()",Py_single_input,fGlobalNS,fLocalNS);
PyObject* pModuleValues=PyDict_GetItemString(fLocalNS,"outputFlat");
std::size_t pOutputModuleSize=(std::size_t)PyList_Size(pModuleValues);
//Testing the actual and expected output tensor sizes
EXPECT_EQ(outputModule.size(), pOutputModuleSize);
//Testing the actual and expected output tensor values
for (size_t i = 0; i < outputModule.size(); ++i) {
float pOutputModule=(float)PyFloat_AsDouble(PyList_GetItem(pModuleValues, i));
EXPECT_LE(std::abs(outputModule[i] - pOutputModule), TOLERANCE);
}
}
TEST(RModelParser_PyTorch, CONVOLUTION_MODEL)
{
constexpr float TOLERANCE = 1.E-3;
std::vector<float> inputConv(5*6*5*5);
std::iota(inputConv.begin(), inputConv.end(), 1.0f);
Py_Initialize();
if (gSystem->AccessPathName("PyTorchModelConvolution.pt",kFileExists))
GenerateModels();
std::vector<size_t> inputTensorShapeConvolution{5, 6, 5, 5};
std::vector<std::vector<size_t>> inputShapesConvolution{inputTensorShapeConvolution};
TMVA::Experimental::RSofieReader s("PyTorchModelConvolution.pt", inputShapesConvolution);
std::vector<float> outputConv = s.Compute(inputConv);
PyObject* main = PyImport_AddModule("__main__");
PyObject* fGlobalNS = PyModule_GetDict(main);
PyObject* fLocalNS = PyDict_New();
if (!fGlobalNS) {
throw std::runtime_error("Can't init global namespace for Python");
}
if (!fLocalNS) {
throw std::runtime_error("Can't init local namespace for Python");
}
PyRun_String("import torch",Py_single_input,fGlobalNS,fLocalNS);
PyRun_String("model=torch.jit.load('PyTorchModelConvolution.pt')",Py_single_input,fGlobalNS,fLocalNS);
PyRun_String("input=torch.arange(1,751,dtype=torch.float).reshape(5,6,5,5)",Py_single_input,fGlobalNS,fLocalNS);
PyRun_String("output=model(input).detach().numpy().reshape(5,5,2,2)",Py_single_input,fGlobalNS,fLocalNS);
PyRun_String("outputFlat=output.flatten().tolist()",Py_single_input,fGlobalNS,fLocalNS);
PyObject* pConvValues=PyDict_GetItemString(fLocalNS,"outputFlat");
std::size_t pOutputConvSize=(std::size_t)PyList_Size(pConvValues);
//Testing the actual and expected output tensor sizes
EXPECT_EQ(outputConv.size(), pOutputConvSize);
//Testing the actual and expected output tensor values
for (size_t i = 0; i < outputConv.size(); ++i) {
float pOutputConv=(float)PyFloat_AsDouble(PyList_GetItem(pConvValues, i));
EXPECT_LE(std::abs(outputConv[i] - pOutputConv), TOLERANCE);
}
}