1 #ifndef PhysicsTools_PatAlgos_BaseMVAValueMapProducer 2 #define PhysicsTools_PatAlgos_BaseMVAValueMapProducer 35 #include "TMVA/Factory.h" 36 #include "TMVA/Reader.h" 63 if (disableONNXGraphOpt) {
64 Ort::SessionOptions sess_opts;
66 sess_opts.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_DISABLE_ALL);
67 ort_ = std::make_unique<cms::Ort::ONNXRuntime>(
model_path, &sess_opts);
79 std::shared_ptr<tensorflow::GraphDef>
graph_;
81 std::unique_ptr<cms::Ort::ONNXRuntime>
ort_;
101 std::vector<edm::ParameterSet>
const& varsPSet = iConfig.
getParameter<std::vector<edm::ParameterSet>>(
"variables");
102 values_.resize(varsPSet.size());
122 for (
const auto&
s : iConfig.
getParameter<std::vector<std::string>>(
"outputFormulas")) {
128 produces<edm::ValueMap<float>>();
131 produces<edm::ValueMap<float>>(
n);
159 std::vector<std::pair<std::string, StringObjectFunction<T, true>>>
funcs_;
177 template <
typename T>
181 readAdditionalCollections(
iEvent, iSetup);
182 std::vector<std::vector<float>> mvaOut((tmva_) ? 1 : output_names_.size());
183 for (
auto&
v : mvaOut)
184 v.reserve(
src->size());
188 std::vector<float>
data;
189 data.reserve(
src->size() * positions_.size());
190 for (
auto const&
o : *
src) {
191 for (
auto const&
p : funcs_) {
194 fillAdditionalVariables(
o);
195 data.insert(
data.end(), values_.begin(), values_.end());
200 tensorflow::TensorShape input_size{(
long long int)
src->size(), (
long long int)positions_.size()};
202 input_tensors.resize(1);
205 for (
unsigned i = 0;
i <
data.size(); ++
i) {
206 input_tensors[0].second.flat<
float>()(
i) =
data[
i];
208 std::vector<tensorflow::Tensor> output_tensors;
209 tensorflow::run(globalCache()->getTFSession(), input_tensors, {outputTensorName_}, &output_tensors);
210 for (
unsigned i = 0;
i < output_tensors.at(0).NumElements(); ++
i) {
211 outputs.push_back(output_tensors.at(0).flat<
float>()(
i));
216 globalCache()->getONNXSession().run({inputTensorName_},
inputs, {}, {outputTensorName_},
src->size())[0];
219 const unsigned outdim =
outputs.size() /
src->size();
220 for (
unsigned i = 0;
i <
src->size(); ++
i) {
221 std::vector<float> tmpOut(
outputs.begin() +
i * outdim,
outputs.begin() + (
i + 1) * outdim);
222 for (
size_t k = 0;
k < output_names_.size();
k++) {
223 mvaOut[
k].push_back(output_formulas_[
k](tmpOut));
228 for (
auto const&
o : *
src) {
229 for (
auto const&
p : funcs_) {
232 fillAdditionalVariables(
o);
234 mvaOut[0].push_back(isClassifier_ ? reader_->EvaluateMVA(name_) : reader_->EvaluateRegression(name_)[0]);
236 std::vector<float> tmpOut;
239 tensorflow::TensorShape input_size{1, (
long long int)positions_.size()};
241 input_tensors.resize(1);
244 for (
size_t j = 0;
j < values_.size();
j++) {
245 input_tensors[0].second.matrix<
float>()(0,
j) = values_[
j];
247 std::vector<tensorflow::Tensor>
outputs;
250 tmpOut.push_back(
outputs.at(0).matrix<
float>()(0,
k));
253 tmpOut = globalCache()->getONNXSession().run({inputTensorName_},
inputs, {}, {outputTensorName_})[0];
255 for (
size_t k = 0;
k < output_names_.size();
k++)
262 for (
auto&
m : mvaOut) {
272 template <
typename T>
275 bool disableONNXGraphOpt =
false;
277 disableONNXGraphOpt =
cfg.getParameter<
bool>(
"disableONNXGraphOpt");
278 return std::make_unique<BaseMVACache>(
282 template <
typename T>
285 template <
typename T>
290 desc.add<
std::string>(
"name")->setComment(
"output score variable name");
291 desc.add<
edm::FileInPath>(
"weightFile")->setComment(
"xml weight file, or TF/ONNX model file");
292 desc.add<
bool>(
"batch_eval",
false)->setComment(
"Run inference in batch instead of per-object");
295 variable.add<
std::string>(
"name")->setComment(
"name of the variable, either created by expr, or internally by code");
297 "a function to define the content of the model input, absence of it means the leaf is computed internally");
298 variable.setComment(
"a PSet to define an entry to the ML model");
302 "inputTensorName",
"",
true,
edm::Comment(
"Name of tensorflow input tensor in the model"));
304 "outputTensorName",
"",
true,
edm::Comment(
"Name of tensorflow output tensor in the model"));
307 std::vector<std::string>(),
309 edm::Comment(
"Names of the output values to be used in the output valuemap"));
312 std::vector<std::string>(),
314 edm::Comment(
"Formulas to be used to post process the output"));
316 "disableONNXGraphOpt",
false,
true,
edm::Comment(
"Disable ONNX runtime graph optimization"));
319 "backend",
"TMVA",
true,
edm::Comment(
"the backend to evaluate the model:tmva, tf or onnx")),
321 "isClassifier",
true,
true,
edm::Comment(
"a classification or regression"))
or 322 "TF" >> (itn and otn and
on and
of)
or "ONNX" >> (itn and otn and
on and
of and dog));
327 template <
typename T>
337 modname +=
"BaseMVAValueMapProducer";
std::vector< float > values_
static edm::ParameterSetDescription getDescription()
void endStream() override
virtual void fillAdditionalVariables(const T &)
std::vector< NamedTensor > NamedTensorList
T getParameter(std::string const &) const
std::vector< StringObjectFunction< std::vector< float > > > output_formulas_
::Ort::SessionOptions defaultSessionOptions(Backend backend=Backend::cpu)
static std::unique_ptr< BaseMVACache > initializeGlobalCache(const edm::ParameterSet &cfg)
std::string inputTensorName_
std::shared_ptr< tensorflow::GraphDef > graph_
GraphDef * loadGraphDef(const std::string &pbFile)
static void globalEndJob(const BaseMVACache *cache)
void produce(edm::Event &, const edm::EventSetup &) override
std::string outputTensorName_
std::string weightfilename_
std::vector< std::vector< float > > FloatArrays
bool setValue(Container &, const reco::JetBaseRef &, const JetExtendedData &)
associate jet with value. Returns false and associate nothing if jet is already associated ...
const cms::Ort::ONNXRuntime & getONNXSession() const
void setValue(const std::string var, float val)
std::pair< std::string, Tensor > NamedTensor
std::vector< std::pair< std::string, StringObjectFunction< T, true > > > funcs_
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
void run(Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const thread::ThreadPoolOptions &threadPoolOptions)
bool closeSession(Session *&session)
tensorflow::Session * getTFSession() const
std::unique_ptr< cms::Ort::ONNXRuntime > ort_
Session * createSession()
edm::EDGetTokenT< edm::View< T > > src_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Analysis-level electron class.
std::map< std::string, size_t > positions_
Analysis-level calorimeter jet class.
void add(std::string const &label, ParameterSetDescription const &psetDescription)
TMVA::IMethod * loadTMVAWeights(TMVA::Reader *reader, const std::string &method, const std::string &weightFile, bool verbose=false)
tensorflow::Session * tf_session_
deadvectors [0] push_back({0.0175431, 0.538005, 6.80997, 13.29})
char data[epos_bytes_allocation]
~BaseMVAValueMapProducer() override
void beginStream(edm::StreamID) override
BaseMVAValueMapProducer(const edm::ParameterSet &iConfig, const BaseMVACache *cache)
uint32_t dimension(pat::CandKinResolution::Parametrization parametrization)
Returns the number of free parameters in a parametrization (3 or 4)
BaseMVACache(const std::string &model_path, const std::string &backend, const bool disableONNXGraphOpt)
Analysis-level muon class.
std::vector< std::string > output_names_
virtual void readAdditionalCollections(edm::Event &, const edm::EventSetup &)
to be implemented in derived classes, filling values for additional variables