28#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
29#include <numpy/arrayobject.h>
32namespace Experimental{
97 throw std::runtime_error(
"TMVA::SOFIE - Parsing PyTorch node " +fNodeType+
" is not yet supported ");
132 fAttrTransA = !fAttrTransB;
136 fAttrTransB = !fAttrTransA;
139 std::unique_ptr<ROperator>
op;
146 throw std::runtime_error(
"TMVA::SOFIE - Unsupported - Operator Gemm does not yet support input type " +
fNodeDType);
166 std::unique_ptr<ROperator>
op;
173 throw std::runtime_error(
"TMVA::SOFIE - Unsupported - Operator Relu does not yet support input type " +
fNodeDType);
192 std::unique_ptr<ROperator>
op;
199 throw std::runtime_error(
"TMVA::SOFIE - Unsupported - Operator Selu does not yet support input type " +
fNodeDType);
218 std::unique_ptr<ROperator>
op;
225 throw std::runtime_error(
"TMVA::SOFIE - Unsupported - Operator Sigmoid does not yet support input type " +
fNodeDType);
254 std::unique_ptr<ROperator>
op;
261 throw std::runtime_error(
"TMVA::SOFIE - Unsupported - Operator Transpose does not yet support input type " +
fNodeDType);
289 std::string fAttrAutopad =
"NOTSET";
290 std::vector<size_t> fAttrDilations = GetDataFromList(
fDilations);
292 std::vector<size_t> fAttrKernelShape = GetDataFromList(
fKernelShape);
293 std::vector<size_t> fAttrPads = GetDataFromList(
fPads);
294 std::vector<size_t> fAttrStrides = GetDataFromList(fStrides);
300 std::unique_ptr<ROperator>
op;
307 throw std::runtime_error(
"TMVA::SOFIE - Unsupported - Operator Conv does not yet support input type " +
fNodeDType);
366 if (
isep != std::string::npos){
371 if(!std::ifstream(
filename).good()){
372 throw std::runtime_error(
"Model file "+
filename_nodir+
" not found!");
376 std::time_t
ttime = std::time(0);
388 throw std::runtime_error(
"Can't init global namespace for Python");
391 throw std::runtime_error(
"Can't init local namespace for Python");
399 PyRunString(
"print('Torch Version: '+torch.__version__)",fGlobalNS,fLocalNS);
400 PyRunString(
"from torch.onnx.utils import _model_to_graph",fGlobalNS,fLocalNS);
403 PyRunString(
"globals().update(locals())",fGlobalNS,fLocalNS);
409 for(
long unsigned int it=0;it<
inputShapes.size();++it){
414 PyRunString(
"dummyInputs.append(torch.rand(*inputShape))",fGlobalNS,fLocalNS);
420 PyRunString(
"graph=_model_to_graph(model,dummyInputs)",fGlobalNS,fLocalNS);
428 " sel = node.kindOf(key)\n"
429 " return getattr(node, sel)(key)\n",
430 fGlobalNS, fLocalNS);
432 " globals().update(locals())\n"
434 " nodeData['nodeType']=i.kind()\n"
435 " nodeAttributeNames=[x for x in i.attributeNames()]\n"
436 " nodeAttributes={j: _node_get(i, j) for j in nodeAttributeNames}\n"
437 " nodeData['nodeAttributes']=nodeAttributes\n"
438 " nodeInputs=[x for x in i.inputs()]\n"
439 " nodeInputNames=[x.debugName() for x in nodeInputs]\n"
440 " nodeData['nodeInputs']=nodeInputNames\n"
441 " nodeOutputs=[x for x in i.outputs()]\n"
442 " nodeOutputNames=[x.debugName() for x in nodeOutputs]\n"
443 " nodeData['nodeOutputs']=nodeOutputNames\n"
444 " nodeDType=[x.type().scalarType() for x in nodeOutputs]\n"
445 " nodeData['nodeDType']=nodeDType\n"
446 " modelData.append(nodeData)",
447 fGlobalNS, fLocalNS);
452 std::string fNodeType;
460 if(fNodeType ==
"onnx::Gemm"){
461 rmodel.AddBlasRoutines({
"Gemm",
"Gemv"});
463 else if(fNodeType ==
"onnx::Selu" || fNodeType ==
"onnx::Sigmoid"){
464 rmodel.AddNeededStdLib(
"cmath");
466 else if (fNodeType ==
"onnx::Conv") {
467 rmodel.AddBlasRoutines({
"Gemm",
"Axpy"});
474 PyRunString(
"weightNames=[k for k in graph[1].keys()]",fGlobalNS,fLocalNS);
475 PyRunString(
"weights=[v.numpy() for v in graph[1].values()]",fGlobalNS,fLocalNS);
476 PyRunString(
"weightDTypes=[v.type()[6:-6] for v in graph[1].values()]",fGlobalNS,fLocalNS);
484 std::size_t fWeightSize;
499 std::shared_ptr<void> fData(
malloc(fWeightSize *
sizeof(
float)), free);
500 std::memcpy(fData.get(),
fWeightValue,fWeightSize *
sizeof(
float));
511 PyRunString(
"inputs=[x for x in model.graph.inputs()]",fGlobalNS,fLocalNS);
512 PyRunString(
"inputs=inputs[1:]",fGlobalNS,fLocalNS);
513 PyRunString(
"inputNames=[x.debugName() for x in inputs]",fGlobalNS,fLocalNS);
516 std::vector<size_t>fInputShape;
535 PyRunString(
"outputs=[x for x in graph[0].outputs()]",fGlobalNS,fLocalNS);
536 PyRunString(
"outputNames=[x.debugName() for x in outputs]",fGlobalNS,fLocalNS);
538 std::vector<std::string> fOutputNames;
542 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 std::vector< size_t > GetDataFromList(PyObject *listObject)
Utility function which retrieves and returns the values of the List object as a vector of size_t.
static const char * PyStringAsString(PyObject *string)
Returns const char* from Python string in PyObject.
void PyRunString(TString code, TString errorMessage="Failed to run python code", int start=256)
Execute Python code from string.
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.
static void(& PyRunString)(TString, PyObject *, PyObject *)
static const char *(& PyStringAsString)(PyObject *)
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)
create variable transformations