38 std::cout <<
"\nPython error message:\n";
40 throw std::runtime_error(
"\nFailed to run python code: " + code);
44const char *PyStringAsString(
PyObject *
string)
118 throw std::runtime_error(
"TMVA::SOFIE - Parsing PyTorch node " +fNodeType+
" is not yet supported ");
153 fAttrTransA = !fAttrTransB;
157 fAttrTransB = !fAttrTransA;
160 std::unique_ptr<ROperator>
op;
167 throw std::runtime_error(
"TMVA::SOFIE - Unsupported - Operator Gemm does not yet support input type " +
fNodeDType);
187 std::unique_ptr<ROperator>
op;
194 throw std::runtime_error(
"TMVA::SOFIE - Unsupported - Operator Relu does not yet support input type " +
fNodeDType);
213 std::unique_ptr<ROperator>
op;
220 throw std::runtime_error(
"TMVA::SOFIE - Unsupported - Operator Selu does not yet support input type " +
fNodeDType);
239 std::unique_ptr<ROperator>
op;
246 throw std::runtime_error(
"TMVA::SOFIE - Unsupported - Operator Sigmoid does not yet support input type " +
fNodeDType);
302 std::string fAttrAutopad =
"NOTSET";
303 std::vector<size_t> fAttrDilations = GetDataFromList(
fDilations);
305 std::vector<size_t> fAttrKernelShape = GetDataFromList(
fKernelShape);
306 std::vector<size_t> fAttrPads = GetDataFromList(fPads);
307 std::vector<size_t> fAttrStrides = GetDataFromList(fStrides);
313 std::unique_ptr<ROperator>
op;
320 throw std::runtime_error(
"TMVA::SOFIE - Unsupported - Operator Conv does not yet support input type " +
fNodeDType);
379 if (
isep != std::string::npos){
384 if(!std::ifstream(
filename).good()){
385 throw std::runtime_error(
"Model file "+
filename_nodir+
" not found!");
389 std::time_t
ttime = std::time(0);
401 throw std::runtime_error(
"Can't init global namespace for Python");
404 throw std::runtime_error(
"Can't init local namespace for Python");
411 PyRunString(
"import torch",fGlobalNS,fLocalNS);
412 PyRunString(
"print('Torch Version: '+torch.__version__)",fGlobalNS,fLocalNS);
413 PyRunString(
"from torch.onnx.utils import _model_to_graph",fGlobalNS,fLocalNS);
416 PyRunString(
"globals().update(locals())",fGlobalNS,fLocalNS);
417 PyRunString(
"model.cpu()",fGlobalNS,fLocalNS);
418 PyRunString(
"model.eval()",fGlobalNS,fLocalNS);
421 PyRunString(
"dummyInputs=[]",fGlobalNS,fLocalNS);
422 for(
long unsigned int it=0;it<
inputShapes.size();++it){
423 PyRunString(
"inputShape=[]",fGlobalNS,fLocalNS);
427 PyRunString(
"dummyInputs.append(torch.rand(*inputShape))",fGlobalNS,fLocalNS);
433 PyRunString(
"graph=_model_to_graph(model,dummyInputs)",fGlobalNS,fLocalNS);
437 PyRunString(
"modelData=[]",fGlobalNS,fLocalNS);
440 PyRunString(
"def _node_get(node, key):\n"
441 " sel = node.kindOf(key)\n"
442 " return getattr(node, sel)(key)\n",
443 fGlobalNS, fLocalNS);
444 PyRunString(
"for i in graph[0].nodes():\n"
445 " globals().update(locals())\n"
447 " nodeData['nodeType']=i.kind()\n"
448 " nodeAttributeNames=[x for x in i.attributeNames()]\n"
449 " nodeAttributes={j: _node_get(i, j) for j in nodeAttributeNames}\n"
450 " nodeData['nodeAttributes']=nodeAttributes\n"
451 " nodeInputs=[x for x in i.inputs()]\n"
452 " nodeInputNames=[x.debugName() for x in nodeInputs]\n"
453 " nodeData['nodeInputs']=nodeInputNames\n"
454 " nodeOutputs=[x for x in i.outputs()]\n"
455 " nodeOutputNames=[x.debugName() for x in nodeOutputs]\n"
456 " nodeData['nodeOutputs']=nodeOutputNames\n"
457 " nodeDType=[x.type().scalarType() for x in nodeOutputs]\n"
458 " nodeData['nodeDType']=nodeDType\n"
459 " modelData.append(nodeData)",
460 fGlobalNS, fLocalNS);
465 std::string fNodeType;
473 if(fNodeType ==
"onnx::Gemm"){
474 rmodel.AddBlasRoutines({
"Gemm",
"Gemv"});
476 else if(fNodeType ==
"onnx::Selu" || fNodeType ==
"onnx::Sigmoid"){
477 rmodel.AddNeededStdLib(
"cmath");
479 else if (fNodeType ==
"onnx::Conv") {
480 rmodel.AddBlasRoutines({
"Gemm",
"Axpy"});
491 PyRunString(
"weightNames=[k for k in graph[1].keys()]",fGlobalNS,fLocalNS);
492 PyRunString(
"weightValues=[v.numpy() for v in graph[1].values()]",fGlobalNS,fLocalNS);
493 PyRunString(
"weightShapes=[list(v.shape) for v in weightValues]",fGlobalNS,fLocalNS);
494 PyRunString(
"weightBytes=[v.tobytes() for v in weightValues]",fGlobalNS,fLocalNS);
495 PyRunString(
"weightDTypes=[v.type()[6:-6] for v in graph[1].values()]",fGlobalNS,fLocalNS);
503 std::size_t fWeightSize;
519 std::shared_ptr<void> fData(
malloc(fWeightSize *
sizeof(
float)), free);
520 std::memcpy(fData.get(),
fWeightValue,fWeightSize *
sizeof(
float));
531 PyRunString(
"inputs=[x for x in model.graph.inputs()]",fGlobalNS,fLocalNS);
532 PyRunString(
"inputs=inputs[1:]",fGlobalNS,fLocalNS);
533 PyRunString(
"inputNames=[x.debugName() for x in inputs]",fGlobalNS,fLocalNS);
536 std::vector<size_t>fInputShape;
555 PyRunString(
"outputs=[x for x in graph[0].outputs()]",fGlobalNS,fLocalNS);
556 PyRunString(
"outputNames=[x.debugName() for x in outputs]",fGlobalNS,fLocalNS);
558 std::vector<std::string> fOutputNames;
562 rmodel.AddOutputTensorNameList(fOutputNames);
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
const_iterator end() const
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
std::unique_ptr< ROperator > MakePyTorchGemm(PyObject *fNode)
Prepares a ROperator_Gemm object.
std::unique_ptr< ROperator > MakePyTorchNode(PyObject *fNode)
Prepares equivalent ROperator with respect to PyTorch ONNX node.
std::unique_ptr< ROperator > MakePyTorchConv(PyObject *fNode)
Prepares a ROperator_Conv object.
std::unique_ptr< ROperator > MakePyTorchSigmoid(PyObject *fNode)
Prepares a ROperator_Sigmoid object.
std::unique_ptr< ROperator > MakePyTorchSelu(PyObject *fNode)
Prepares a ROperator_Selu object.
std::unique_ptr< ROperator > MakePyTorchRelu(PyObject *fNode)
Prepares a ROperator_Relu object.
const PyTorchMethodMap mapPyTorchNode
std::unordered_map< std::string, std::unique_ptr< ROperator >(*)(PyObject *fNode)> PyTorchMethodMap
std::unique_ptr< ROperator > MakePyTorchTranspose(PyObject *fNode)
Prepares a ROperator_Transpose object.
RModel Parse(std::string filepath, std::vector< std::vector< size_t > > inputShapes, std::vector< ETensorType > dtype)
Parser function for translating PyTorch .pt model into a RModel object.
std::string ConvertTypeToString(ETensorType type)
ETensorType ConvertStringToType(std::string type)