ATLAS Offline Software
|
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration. More...
#include <TFCSONNXHandler.h>
Public Types | |
typedef std::map< std::string, std::map< std::string, double > > | NetworkInputs |
Format for network inputs. More... | |
typedef std::map< std::string, double > | NetworkOutputs |
Format for network outputs. More... | |
Public Member Functions | |
TFCSONNXHandler (const std::string &inputFile) | |
TFCSONNXHandler constructor. More... | |
TFCSONNXHandler (const std::vector< char > &bytes) | |
TFCSONNXHandler constructor. More... | |
TFCSONNXHandler (const TFCSONNXHandler ©_from) | |
TFCSONNXHandler copy constructor. More... | |
NetworkOutputs | compute (NetworkInputs const &inputs) const override |
Function to pass values to the network. More... | |
void | writeNetToTTree (TTree &tree) override |
Save the network to a TTree. More... | |
std::vector< std::string > | getOutputLayers () const override |
List the names of the outputs. More... | |
void | deleteAllButNet () override |
Get rid of any memory objects that arn't needed to run the net. More... | |
template<typename Tin , typename Tout > | |
VNetworkBase::NetworkOutputs | computeTemplate (VNetworkBase::NetworkInputs const &inputs) |
VNetworkBase () | |
VNetworkBase default constructor. More... | |
VNetworkBase (const std::string &inputFile) | |
VNetworkBase constructor. More... | |
VNetworkBase (const VNetworkBase ©_from) | |
VNetworkBase copy constructor. More... | |
virtual void | writeNetToTTree (TTree &tree)=0 |
Save the network to a TTree. More... | |
void | writeNetToTTree (TFile &root_file, std::string const &tree_name=m_defaultTreeName) |
Save the network to a TTree. More... | |
void | writeNetToTTree (std::string const &root_name, std::string const &tree_name=m_defaultTreeName) |
Save the network to a TTree. More... | |
void | writeNetToTTree (TFile &root_file, std::string const &tree_name=m_defaultTreeName) |
Save the network to a TTree. More... | |
void | writeNetToTTree (std::string const &root_name, std::string const &tree_name=m_defaultTreeName) |
Save the network to a TTree. More... | |
bool | isFile () const |
Check if the argument inputFile is the path of a file on disk. More... | |
bool | msgLvl (const MSG::Level lvl) const |
Check whether the logging system is active at the provided verbosity level. More... | |
MsgStream & | msg () const |
Return a stream for sending messages directly (no decoration) More... | |
MsgStream & | msg (const MSG::Level lvl) const |
Return a decorated starting stream for sending messages. More... | |
MSG::Level | level () const |
Retrieve output level. More... | |
virtual void | setLevel (MSG::Level lvl) |
Update outputlevel. More... | |
Static Public Member Functions | |
static std::string | representNetworkInputs (NetworkInputs const &inputs, int maxValues=3) |
String representation of network inputs. More... | |
static std::string | representNetworkOutputs (NetworkOutputs const &outputs, int maxValues=3) |
String representation of network outputs. More... | |
static bool | isFile (std::string const &inputFile) |
Check if a string is the path of a file on disk. More... | |
static std::string | startMsg (MSG::Level lvl, const std::string &file, int line) |
Make a message to decorate the start of logging. More... | |
Static Public Attributes | |
static const std::string | m_defaultTreeName = "onnxruntime_session" |
Default name for the TTree to save in. More... | |
Protected Member Functions | |
virtual void | print (std::ostream &strm) const override |
Write a short description of this net to the string stream. More... | |
void | setupPersistedVariables () override |
Perform actions that prep data to create the net. More... | |
void | setupNet () override |
Perform actions that prepare network for use. More... | |
bool | isRootFile (std::string const &filename="") const |
Check if a string is possibly a root file path. More... | |
void | removePrefixes (NetworkOutputs &outputs) const |
Remove any common prefix from the outputs. More... | |
void | removePrefixes (std::vector< std::string > &output_names) const |
Remove any common prefix from the outputs. More... | |
Protected Attributes | |
std::string | m_inputFile |
Path to the file describing the network, including filename. More... | |
Private Member Functions | |
std::vector< char > | getSerializedSession (const std::string &tree_name=m_defaultTreeName) |
Return content of the proto (.onnx) file in memory. More... | |
std::vector< char > | readBytesFromTTree (TTree &tree) |
Retrieve the content of the proto file from a TTree. More... | |
void | writeBytesToTTree (TTree &tree, const std::vector< char > &bytes) |
Write the content of the proto file to a TTree as a branch. More... | |
void | readSerializedSession () |
Do not persistify. More... | |
template<typename Tin , typename Tout > | |
NetworkOutputs | computeTemplate (NetworkInputs const &input) |
Do not persistify. More... | |
ClassDefOverride (TFCSONNXHandler, 1) | |
Do not persistify. More... | |
ClassDef (VNetworkBase, 1) | |
Private Attributes | |
std::vector< char > | m_bytes |
Content of the proto file. More... | |
std::unique_ptr< Ort::Session > | m_session |
The network session itself. More... | |
std::vector< const char * > | m_inputNodeNames |
names that index the input nodes More... | |
std::vector< const char * > | m_outputNodeNames |
Do not persistify. More... | |
std::vector< std::vector< int64_t > > | m_inputNodeDims |
Do not persistify. More... | |
std::vector< std::vector< int64_t > > | m_outputNodeDims |
Do not persistify. More... | |
std::vector< int64_t > | m_outputNodeSize |
Do not persistify. More... | |
std::function< NetworkOutputs(NetworkInputs)> | m_computeLambda |
computeTemplate with apropreate types selected. More... | |
Ort::MemoryInfo | m_memoryInfo |
Do not persistify. More... | |
std::vector< std::string > | m_outputLayers |
Do not persistify. More... | |
std::string | m_nm |
Message source name. More... | |
Static Private Attributes | |
static boost::thread_specific_ptr< MsgStream > m_msg_tls | ATLAS_THREAD_SAFE |
Do not persistify! More... | |
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration.
Class for a neural network read in the ONNX format. Derived from the abstract base class VNetworkBase such that it can be used interchangably with it's sibling classes, TFCSSimpleLWTNNHandler and TFCSGANLWTNNHandler.
The TFCSNetworkFactory::Create function has VNetworkBase as its return type so that it can make a run-time decision about which derived class to use, based on the data or file presented. As such it's best not to create this directly, instead allow TFCSNetworkFactory::Create to create the appropreat network object so that new network formats can be accomidated by writing new subclasses of VNetworkBase.
A handler specific for an ONNX network
Inherits from the generic interface VNetworkBase, such that it cna be used interchangably with other network formats and libraries.
Definition at line 43 of file TFCSONNXHandler.h.
|
inherited |
Format for network inputs.
The doubles are the values to be passed into the network. Strings in the outer map identify the input node, which must corrispond to the names of the nodes as read from the description of the network found by the constructor. Strings in the inner map identify the part of the input node, for some networks these must be simple integers, in string form, as parts of nodes do not always have the ability to carry real string labels.
Definition at line 90 of file VNetworkBase.h.
|
inherited |
Format for network outputs.
The doubles are the values generated by the network. Strings identify which node this value came from, and when nodes have multiple values, are suffixed with a number to indicate which part of the node they came from. So in multi-value nodes the format becomes "<node_name>_<part_n>"
Definition at line 100 of file VNetworkBase.h.
|
explicit |
TFCSONNXHandler constructor.
Calls setupPersistedVariables and setupNet.
inputFile | file-path on disk (with file name) of a readable onnx file containing a proto format description of the network to be constructed. |
Definition at line 24 of file TFCSONNXHandler.cxx.
|
explicit |
TFCSONNXHandler constructor.
As this passes nothing to the super constructor the setupPersistedVariables will not be called.
bytes | byte content of a .onnx file, (which are a subset if proto files). Allows TFCSONNXHandler objects to be created from data in memory, retrived rom any source. The bytes are not copied interally, and must remain in memory while the net is in use. (TODO check that assertion) |
Definition at line 32 of file TFCSONNXHandler.cxx.
TFCSONNXHandler::TFCSONNXHandler | ( | const TFCSONNXHandler & | copy_from | ) |
TFCSONNXHandler copy constructor.
Will copy the variables taht would be generated by setupPersistedVariables and setupNet.
copy_from | existing network that we are copying |
Definition at line 41 of file TFCSONNXHandler.cxx.
|
privateinherited |
|
private |
Do not persistify.
|
overridevirtual |
Function to pass values to the network.
This function is used to actually run data through the loaded network and obtain results.
inputs | values to be evaluated by the network |
Implements VNetworkBase.
Definition at line 57 of file TFCSONNXHandler.cxx.
|
private |
Do not persistify.
Computation template with adjustable types for input.
A lambda function will be used to make the correct type choice for the session/net used as a member variable during setupNet.
VNetworkBase::NetworkOutputs TFCSONNXHandler::computeTemplate | ( | VNetworkBase::NetworkInputs const & | inputs | ) |
Definition at line 338 of file TFCSONNXHandler.cxx.
|
overridevirtual |
Get rid of any memory objects that arn't needed to run the net.
Minimise memory usage by deleting nay inputs that are no longer required to run the compute function. Doesn't actually do anything for this network type.
Implements VNetworkBase.
Definition at line 72 of file TFCSONNXHandler.cxx.
|
overridevirtual |
List the names of the outputs.
Outputs are stored in an NetworkOutputs object which is indexed by strings. This function returns the list of all strings that will index the outputs.
Implements VNetworkBase.
Definition at line 67 of file TFCSONNXHandler.cxx.
|
private |
Return content of the proto (.onnx) file in memory.
Get the session as a stream of bytes It's a vector<char> rather than a string becuase we need the guarantee that &bytes[0]+n == bytes[n] (string has this only after c++11). Also bytes may not be terminated by a null byte (which early strings required).
Definition at line 273 of file TFCSONNXHandler.cxx.
|
inherited |
Check if the argument inputFile is the path of a file on disk.
Determines if the string that was passed to the constructor as inputFile corrisponds to tha path of a file that can be read on the disk.
Definition at line 117 of file VNetworkBase.cxx.
|
staticinherited |
Check if a string is the path of a file on disk.
Determines if a string corrisponds to tha path of a file that can be read on the disk.
inputFile | name of the pottential file |
Definition at line 119 of file VNetworkBase.cxx.
|
protectedinherited |
Check if a string is possibly a root file path.
Just checks if the string ends in .root as there are almost no reliable rules for file paths.
inputFile | name of the pottential file if blank, m_inputFile is used. |
Definition at line 101 of file VNetworkBase.cxx.
|
inlineinherited |
|
inlineinherited |
Return a stream for sending messages directly (no decoration)
Definition at line 231 of file MLogging.h.
|
inlineinherited |
Return a decorated starting stream for sending messages.
Definition at line 240 of file MLogging.h.
|
inlineinherited |
Check whether the logging system is active at the provided verbosity level.
Definition at line 222 of file MLogging.h.
|
overrideprotectedvirtual |
Write a short description of this net to the string stream.
Specialised for ONNX to print the input and output nodes with their dimensions.
strm | output parameter, to which the description will be written. |
Reimplemented from VNetworkBase.
Definition at line 78 of file TFCSONNXHandler.cxx.
|
private |
Retrieve the content of the proto file from a TTree.
If the ONNX file has been saved as a loose variable in a TTree this method will read it back into m_bytes.
Definition at line 297 of file TFCSONNXHandler.cxx.
|
private |
Do not persistify.
Using content of the proto (.onnx) file make a session.
The m_session variable is initialised from the m_bytes variable so that the net can be run. Requires that the m_bytes variable is retained while the net is used.
Definition at line 324 of file TFCSONNXHandler.cxx.
|
protectedinherited |
Remove any common prefix from the outputs.
outputs | The outputs, changed in place. |
Definition at line 151 of file VNetworkBase.cxx.
|
protectedinherited |
Remove any common prefix from the outputs.
outputs | The output names, changed in place. |
Definition at line 144 of file VNetworkBase.cxx.
|
staticinherited |
String representation of network inputs.
Create a string that summarises a set of network inputs. Gives basic dimensions plus a few values, up to the maxValues
inputs | values to be evaluated by the network |
maxValues | maximum number of values to include in the representaiton |
Definition at line 37 of file VNetworkBase.cxx.
|
staticinherited |
String representation of network outputs.
Create a string that summarises a set of network outputs. Gives basic dimensions plus a few values, up to the maxValues
outputs | output of the network |
maxValues | maximum number of values to include in the representaiton |
Definition at line 57 of file VNetworkBase.cxx.
|
virtualinherited |
|
overrideprotectedvirtual |
Perform actions that prepare network for use.
Will be called in the streamer or class constructor after the inputs have been set (either automaically by the streamer or by setupPersistedVariables in the constructor). Does not delete any resources used.
Implements VNetworkBase.
Definition at line 112 of file TFCSONNXHandler.cxx.
|
overrideprotectedvirtual |
Perform actions that prep data to create the net.
Will be called in the class constructor before calling setupNet, but not in the streamer. It sets any variables that the sreamer would persist when saving or loading to file.
Implements VNetworkBase.
Definition at line 102 of file TFCSONNXHandler.cxx.
|
staticinherited |
Make a message to decorate the start of logging.
Print a message for the start of logging.
Definition at line 116 of file MLogging.cxx.
VNetworkBase::VNetworkBase |
VNetworkBase default constructor.
For use in streamers.
Definition at line 45 of file VNetworkBase.cxx.
|
explicit |
VNetworkBase constructor.
Only saves inputFile to m_inputFile; Inherting classes should call setupPersistedVariables and setupNet in constructor;
inputFile | file-path on disk (with file name) of a readable file containing a description of the network to be constructed or the content of the file. |
Definition at line 59 of file VNetworkBase.cxx.
VNetworkBase::VNetworkBase |
VNetworkBase copy constructor.
Does not call setupPersistedVariables or setupNet but will pass on m_inputFile. Inherting classes should do whatever they need to move the variables created in the setup functions.
copy_from | existing network that we are copying |
Definition at line 71 of file VNetworkBase.cxx.
|
private |
Write the content of the proto file to a TTree as a branch.
The ONNX proto file is saved as a simple branch (no streamers involved).
Definition at line 310 of file TFCSONNXHandler.cxx.
void VNetworkBase::writeNetToTTree |
Save the network to a TTree.
All data required to recreate the network object is saved into a TTree. The format is not specified.
root_name | The path of the file to save inside. |
tree_name | The name of the TTree to save inside. |
Definition at line 196 of file VNetworkBase.cxx.
|
inherited |
Save the network to a TTree.
All data required to recreate the network object is saved into a TTree. The format is not specified.
root_name | The path of the file to save inside. |
tree_name | The name of the TTree to save inside. |
Definition at line 93 of file VNetworkBase.cxx.
void VNetworkBase::writeNetToTTree |
Save the network to a TTree.
All data required to recreate the network object is saved into a TTree. The format is not specified.
root_file | The file to save inside. |
tree_name | The name of the TTree to save inside. |
Definition at line 184 of file VNetworkBase.cxx.
|
inherited |
Save the network to a TTree.
All data required to recreate the network object is saved into a TTree. The format is not specified.
root_file | The file to save inside. |
tree_name | The name of the TTree to save inside. |
Definition at line 83 of file VNetworkBase.cxx.
|
overridevirtual |
Save the network to a TTree.
All data required to recreate the network object is saved into a TTree. The format is not specified. Will still work even if deleteAllButNet has already been called.
tree | The tree to save inside. |
Implements VNetworkBase.
Definition at line 62 of file TFCSONNXHandler.cxx.
virtual void VNetworkBase::writeNetToTTree |
Save the network to a TTree.
All data required to recreate the network object is saved into a TTree. The format is not specified.
tree | The tree to save inside. |
|
inlinestaticprivateinherited |
Do not persistify!
MsgStream instance (a std::cout like with print-out levels)
Definition at line 215 of file MLogging.h.
|
private |
Content of the proto file.
Definition at line 170 of file TFCSONNXHandler.h.
|
private |
computeTemplate with apropreate types selected.
Definition at line 307 of file TFCSONNXHandler.h.
|
inlinestaticinherited |
Default name for the TTree to save in.
Definition at line 173 of file VNetworkBase.h.
|
protectedinherited |
Path to the file describing the network, including filename.
Definition at line 245 of file VNetworkBase.h.
|
private |
Do not persistify.
dimension lengths in each named input node
Describes the shape of the input nodes.
Definition at line 276 of file TFCSONNXHandler.h.
|
private |
names that index the input nodes
An ONNX network is capable of having two layers of labels, input node names, then labels within each node, but it's twin, LWTNN is not. LWTNN supports one list of nodes indexed by strings, and each input node may have more than one value, indexed by positive integers (list like), so this interfae only supports that more limited format.
Definition at line 234 of file TFCSONNXHandler.h.
|
private |
Do not persistify.
Specifies memory behavior for vectors in ONNX.
Definition at line 312 of file TFCSONNXHandler.h.
|
privateinherited |
Message source name.
Definition at line 211 of file MLogging.h.
|
private |
Do not persistify.
Externally visible names that index the output.
Definition at line 318 of file TFCSONNXHandler.h.
|
private |
Do not persistify.
dimension lengths in each named output node
As the final output must be flat in each output node, this is for internal manipulations only.
Definition at line 284 of file TFCSONNXHandler.h.
|
private |
Do not persistify.
the names that index the output nodes
An ONNX network is capable of having two layers of labels, input node names, then labels within each node, but it's twin, LWTNN is not. LWTNN supports one list of nodes indexed by strings, and each input node may have more than one value, indexed by positive integers (list like), so this interfae only supports that more limited format.
Definition at line 258 of file TFCSONNXHandler.h.
|
private |
Do not persistify.
total elements in each named output node
For internal use only, gives the total number of elements in the output nodes.
Definition at line 292 of file TFCSONNXHandler.h.
|
private |
The network session itself.
This is the object created by onnxruntime_cxx_api which contains information about the network and can run inputs through it.
Held as a unique pointer to prevent the need for manual memory management
Definition at line 210 of file TFCSONNXHandler.h.