99 std::ifstream file(
filePath, std::ios::binary);
101 std::ostringstream os;
102 os <<
"failed to open file '" <<
filePath <<
"'";
103 throw std::runtime_error(os.str());
106 file.seekg(0, std::ios::end);
107 const std::streamsize
size = file.tellg();
108 file.seekg(0, std::ios::beg);
111 std::ostringstream os;
112 os <<
"file '" <<
filePath <<
"' is empty";
113 throw std::runtime_error(os.str());
116 std::vector<std::uint8_t>
bytes(
static_cast<std::size_t
>(
size));
117 file.read(
reinterpret_cast<char *
>(
bytes.data()),
size);
120 std::ostringstream os;
121 os <<
"error while reading file '" <<
filePath <<
"'";
122 throw std::runtime_error(os.str());
128template <
typename Fn>
131 static Fn fn =
nullptr;
132 static std::once_flag
flag;
134 std::call_once(
flag, [&] {
137 throw std::runtime_error(std::string(
"ROOT JIT Declare failed for code defining ") + name);
144 throw std::runtime_error(std::string(
"ROOT JIT failed to resolve symbol: ") +
name);
150 throw std::runtime_error(std::string(
"ROOT JIT produced null function pointer for: ") +
name);
160 return TString::Format(
"reinterpret_cast<%s>(0x%zx)", (
castType +
"*").c_str(),
reinterpret_cast<std::size_t
>(ptr))
172 for (std::size_t
j = 0;
j < i; ++
j) {
175 out +=
"inputTensorDims[" + std::to_string(
j) +
"].total_size()";
191 using Func = void (*)(
void *,
float *,
float const *);
216 const std::string &
onnxFile,
const std::vector<std::string> & ,
217 const std::vector<std::vector<int>> & )
223 std::string
istr = std::to_string(i);
225 std::make_unique<RooListProxy>((
"!inputs_" +
istr).c_str(), (
"Input tensor " +
istr).c_str(),
this));
233 for (std::size_t i = 0; i <
other._inputTensors.size(); ++i) {
234 _inputTensors.emplace_back(std::make_unique<RooListProxy>(
"!inputs",
this, *
other._inputTensors[i]));
256 _runtime = std::make_unique<RuntimeCache>();
261 throw std::runtime_error(
"RooONNXFunc: cannot load ONNX file since SOFIE ONNX parser is missing."
262 " Please build ROOT with tmva-sofie=ON.");
264 using OnnxToCpp = std::string (*)(std::uint8_t
const *, std::size_t,
const char *);
267#include "TMVA/RModelParser_ONNX.hxx"
269std::string _RooONNXFunc_onnxToCppWithSofie(std::uint8_t const *onnxBytes, std::size_t onnxBytesSize, const char *outputName)
271 namespace SOFIE = TMVA::Experimental::SOFIE;
273 std::string buffer{reinterpret_cast<const char *>(onnxBytes), onnxBytesSize};
274 std::istringstream stream{buffer};
276 SOFIE::RModel rmodel = SOFIE::RModelParser_ONNX{}.Parse(stream, outputName);
277 rmodel.SetOptimizationLevel(SOFIE::OptimizationLevel::kBasic);
278 rmodel.Generate(SOFIE::Options::kNoWeightFile);
280 std::stringstream ss{};
281 rmodel.PrintGenerated(ss);
286 static int counter = 0;
287 _funcName =
"roo_onnx_func_" + std::to_string(counter);
303 std::string
istr = std::to_string(i);
318 std::ostringstream
ss;
320 <<
"float roo_inner_wrapper(Session const &session, " <<
innerParams <<
") {\n"
321 <<
" float out = 0.;\n"
322 <<
" doInfer(session, " <<
innerArgs <<
", &out);\n"
325 <<
"float roo_wrapper(Session const &session, " <<
innerParams <<
") {\n"
326 <<
" return roo_inner_wrapper(session, " <<
innerArgs <<
");\n"
336 std::ostringstream
ss;
338 <<
"void roo_eval_thunk(void *session_void, float *out, float const *flat_input) {\n"
339 <<
" auto *session = reinterpret_cast<Session *>(session_void);\n"
340 <<
" doInfer(*session";
356 (
"static_cast<void(*)(void *, float *, float const *)>(" +
namespaceName +
"::roo_eval_thunk);").c_str()));
362 gInterpreter->Declare(
"#include <Math/CladDerivator.h>");
370 std::ostringstream
ss;
373 <<
" auto &session = *" <<
ptrSession <<
";\n";
375 ss <<
" float inputFlt" << i <<
"[inputTensorDims[" << i <<
"].total_size()];\n"
376 <<
" for (std::size_t i = 0; i < std::size(inputFlt" << i <<
"); ++i) {\n"
377 <<
" inputFlt" << i <<
"[i] = input" << i <<
"[i];\n"
380 ss <<
" return roo_inner_wrapper(session";
382 ss <<
", inputFlt" << i;
387 <<
"namespace clad::custom_derivatives {\n"
391 ss <<
", double *d_input" << i;
396 ss <<
" float inputFlt" << i <<
"[inputTensorDims[" << i <<
"].total_size()];\n"
397 <<
" float d_inputFlt" << i <<
"[::std::size(inputFlt" << i <<
")];\n"
398 <<
" for (::std::size_t i = 0; i < ::std::size(inputFlt" << i <<
"); ++i) {\n"
399 <<
" inputFlt" << i <<
"[i] = input" << i <<
"[i];\n"
400 <<
" d_inputFlt" << i <<
"[i] = d_input" << i <<
"[i];\n"
405 <<
" roo_inner_wrapper_pullback(*session";
407 ss <<
", inputFlt" << i;
409 ss <<
", d_y, d_session";
411 ss <<
", d_inputFlt" << i;
415 ss <<
" for (::std::size_t i = 0; i < ::std::size(inputFlt" << i <<
"); ++i) {\n"
416 <<
" d_input" << i <<
"[i] += d_inputFlt" << i <<
"[i];\n"
421 <<
"} // namespace clad::custom_derivatives\n";
432 return static_cast<double>(out);
437 if (
R__b.IsReading()) {
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
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 Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t bytes
R__EXTERN TSystem * gSystem
friend void RooRefArray::Streamer(TBuffer &)
Abstract base class for objects that represent a real value and implements functionality common to al...
RooONNXFunc wraps an ONNX model as a RooAbsReal, allowing it to be used as a building block in likeli...
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
std::vector< std::uint8_t > _onnxBytes
Persisted ONNX model bytes.
std::shared_ptr< RuntimeCache > _runtime
! Transient runtime information.
std::vector< float > _inputBuffer
!
std::size_t nInputTensors() const
std::vector< std::unique_ptr< RooListProxy > > _inputTensors
Inputs mapping to flattened input tensors.
void initialize()
Build transient runtime backend on first use.
void fillInputBuffer() const
Gather current RooFit inputs into a contiguous feature buffer.
Buffer base class used for serializing objects.
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
RooFit::Detail::AnyWithVoidPtr _d_session
RooFit::Detail::AnyWithVoidPtr _session