diff --git a/PhysicsTools/ONNXRuntime/BuildFile.xml b/PhysicsTools/ONNXRuntime/BuildFile.xml index 74335c38157af..28c17eb4f0273 100644 --- a/PhysicsTools/ONNXRuntime/BuildFile.xml +++ b/PhysicsTools/ONNXRuntime/BuildFile.xml @@ -1,5 +1,7 @@ + + diff --git a/PhysicsTools/ONNXRuntime/interface/ONNXSessionOptions.h b/PhysicsTools/ONNXRuntime/interface/ONNXSessionOptions.h new file mode 100644 index 0000000000000..f43bc739d11dc --- /dev/null +++ b/PhysicsTools/ONNXRuntime/interface/ONNXSessionOptions.h @@ -0,0 +1,32 @@ +#ifndef PHYSICSTOOLS_ONNXRUNTIME_ONNXSESSIONOPTIONS_H +#define PHYSICSTOOLS_ONNXRUNTIME_ONNXSESSIONOPTIONS_H + +#include "HeterogeneousCore/CUDAServices/interface/CUDAService.h" +#include "FWCore/ServiceRegistry/interface/Service.h" +#include "PhysicsTools/ONNXRuntime/interface/ONNXRuntime.h" +#include "onnxruntime/core/session/onnxruntime_cxx_api.h" +#include + +namespace cms::Ort { + + // param_backend + // cpu -> Use CPU backend + // cuda -> Use cuda backend + // default -> Use best available + inline ::Ort::SessionOptions getSessionOptions(const std::string ¶m_backend) { + auto backend = cms::Ort::Backend::cpu; + if (param_backend == "cuda") + backend = cms::Ort::Backend::cuda; + + if (param_backend == "default") { + edm::Service cs; + if (cs.isAvailable() and cs->enabled()) { + backend = cms::Ort::Backend::cuda; + } + } + + return ONNXRuntime::defaultSessionOptions(backend); + } +} // namespace cms::Ort + +#endif diff --git a/PhysicsTools/ONNXRuntime/src/ONNXRuntime.cc b/PhysicsTools/ONNXRuntime/src/ONNXRuntime.cc index 130e4544585b9..60ddb44b6e436 100644 --- a/PhysicsTools/ONNXRuntime/src/ONNXRuntime.cc +++ b/PhysicsTools/ONNXRuntime/src/ONNXRuntime.cc @@ -20,7 +20,11 @@ namespace cms::Ort { using namespace ::Ort; +#ifdef ONNXDebug + const Env ONNXRuntime::env_(ORT_LOGGING_LEVEL_INFO, ""); +#else const Env ONNXRuntime::env_(ORT_LOGGING_LEVEL_ERROR, ""); +#endif ONNXRuntime::ONNXRuntime(const std::string& model_path, const SessionOptions* session_options) { // create session @@ -80,10 +84,12 @@ namespace cms::Ort { SessionOptions sess_opts; sess_opts.SetIntraOpNumThreads(1); if (backend == Backend::cuda) { - // https://www.onnxruntime.ai/docs/reference/execution-providers/CUDA-ExecutionProvider.html OrtCUDAProviderOptions options; sess_opts.AppendExecutionProvider_CUDA(options); } +#ifdef ONNX_PROFILE + sess_opts.EnableProfiling("ONNXProf"); +#endif return sess_opts; } @@ -140,6 +146,7 @@ namespace cms::Ort { } // run + auto output_tensors = session_->Run(RunOptions{nullptr}, input_node_names_.data(), input_tensors.data(), diff --git a/RecoBTag/ONNXRuntime/plugins/BoostedJetONNXJetTagsProducer.cc b/RecoBTag/ONNXRuntime/plugins/BoostedJetONNXJetTagsProducer.cc index 5cdf4ed1ee962..d51d2811be1a0 100644 --- a/RecoBTag/ONNXRuntime/plugins/BoostedJetONNXJetTagsProducer.cc +++ b/RecoBTag/ONNXRuntime/plugins/BoostedJetONNXJetTagsProducer.cc @@ -14,7 +14,7 @@ #include "DataFormats/BTauReco/interface/DeepBoostedJetTagInfo.h" #include "PhysicsTools/ONNXRuntime/interface/ONNXRuntime.h" - +#include "PhysicsTools/ONNXRuntime/interface/ONNXSessionOptions.h" #include "RecoBTag/FeatureTools/interface/deep_helpers.h" #include @@ -126,12 +126,20 @@ void BoostedJetONNXJetTagsProducer::fillDescriptions(edm::ConfigurationDescripti "probQCDothers", }); desc.addOptionalUntracked("debugMode", false); + desc.add("onnx_backend", "default"); descriptions.addWithDefaultLabel(desc); } std::unique_ptr BoostedJetONNXJetTagsProducer::initializeGlobalCache(const edm::ParameterSet &iConfig) { - return std::make_unique(iConfig.getParameter("model_path").fullPath()); + std::string backend = iConfig.getParameter("onnx_backend"); + + auto session_options = cms::Ort::getSessionOptions(backend); + // Sept 8, 2022 - on gpu, this model crashes with all optimizations on + if (backend != "cpu") + session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_BASIC); + return std::make_unique(iConfig.getParameter("model_path").fullPath(), + &session_options); } void BoostedJetONNXJetTagsProducer::globalEndJob(const ONNXRuntime *cache) {} diff --git a/RecoBTag/ONNXRuntime/plugins/DeepCombinedONNXJetTagsProducer.cc b/RecoBTag/ONNXRuntime/plugins/DeepCombinedONNXJetTagsProducer.cc index 99193460081a7..d1a43103702d1 100644 --- a/RecoBTag/ONNXRuntime/plugins/DeepCombinedONNXJetTagsProducer.cc +++ b/RecoBTag/ONNXRuntime/plugins/DeepCombinedONNXJetTagsProducer.cc @@ -14,7 +14,7 @@ #include "DataFormats/BTauReco/interface/DeepFlavourTagInfo.h" #include "PhysicsTools/ONNXRuntime/interface/ONNXRuntime.h" - +#include "PhysicsTools/ONNXRuntime/interface/ONNXSessionOptions.h" #include "RecoBTag/ONNXRuntime/interface/tensor_fillers.h" #include "RecoBTag/ONNXRuntime/interface/tensor_configs.h" @@ -136,12 +136,15 @@ void DeepCombinedONNXJetTagsProducer::fillDescriptions(edm::ConfigurationDescrip desc.add>("flav_names", std::vector{"probb", "probc", "probuds", "probg"}); desc.add("min_jet_pt", 15.0); desc.add("max_jet_eta", 2.5); + desc.add("onnx_backend", "default"); descriptions.add("pfDeepCombinedJetTags", desc); } std::unique_ptr DeepCombinedONNXJetTagsProducer::initializeGlobalCache(const edm::ParameterSet& iConfig) { - return std::make_unique(iConfig.getParameter("model_path").fullPath()); + auto session_options = cms::Ort::getSessionOptions(iConfig.getParameter("onnx_backend")); + return std::make_unique(iConfig.getParameter("model_path").fullPath(), + &session_options); } void DeepCombinedONNXJetTagsProducer::globalEndJob(const ONNXRuntime* cache) {} diff --git a/RecoBTag/ONNXRuntime/plugins/DeepDoubleXONNXJetTagsProducer.cc b/RecoBTag/ONNXRuntime/plugins/DeepDoubleXONNXJetTagsProducer.cc index 2c73eaac090fc..c6360d4f59cab 100644 --- a/RecoBTag/ONNXRuntime/plugins/DeepDoubleXONNXJetTagsProducer.cc +++ b/RecoBTag/ONNXRuntime/plugins/DeepDoubleXONNXJetTagsProducer.cc @@ -14,7 +14,7 @@ #include "DataFormats/BTauReco/interface/DeepDoubleXTagInfo.h" #include "PhysicsTools/ONNXRuntime/interface/ONNXRuntime.h" - +#include "PhysicsTools/ONNXRuntime/interface/ONNXSessionOptions.h" #include #include #include @@ -121,6 +121,8 @@ void DeepDoubleXONNXJetTagsProducer::fillDescriptions(edm::ConfigurationDescript "CvB" >> (PDPSD("flav_names", std::vector{"probHbb", "probHcc"}, true) and PDFIP("model_path", FIP("RecoBTag/Combined/data/DeepDoubleX/94X/V01/DDCvB.onnx"), true)); }; + desc.add("onnx_backend", "default"); + auto descBvL(desc); descBvL.ifValue(edm::ParameterDescription("flavor", "BvL", true), flavorCases()); descriptions.add("pfDeepDoubleBvLJetTags", descBvL); @@ -135,7 +137,9 @@ void DeepDoubleXONNXJetTagsProducer::fillDescriptions(edm::ConfigurationDescript } std::unique_ptr DeepDoubleXONNXJetTagsProducer::initializeGlobalCache(const edm::ParameterSet& iConfig) { - return std::make_unique(iConfig.getParameter("model_path").fullPath()); + auto session_options = cms::Ort::getSessionOptions(iConfig.getParameter("onnx_backend")); + return std::make_unique(iConfig.getParameter("model_path").fullPath(), + &session_options); } void DeepDoubleXONNXJetTagsProducer::globalEndJob(const ONNXRuntime* cache) {} diff --git a/RecoBTag/ONNXRuntime/plugins/DeepFlavourONNXJetTagsProducer.cc b/RecoBTag/ONNXRuntime/plugins/DeepFlavourONNXJetTagsProducer.cc index 10dacfcd5e819..7fe7b96cb3007 100644 --- a/RecoBTag/ONNXRuntime/plugins/DeepFlavourONNXJetTagsProducer.cc +++ b/RecoBTag/ONNXRuntime/plugins/DeepFlavourONNXJetTagsProducer.cc @@ -14,6 +14,7 @@ #include "DataFormats/BTauReco/interface/DeepFlavourTagInfo.h" #include "PhysicsTools/ONNXRuntime/interface/ONNXRuntime.h" +#include "PhysicsTools/ONNXRuntime/interface/ONNXSessionOptions.h" using namespace cms::Ort; @@ -84,12 +85,15 @@ void DeepFlavourONNXJetTagsProducer::fillDescriptions(edm::ConfigurationDescript desc.add>("output_names", {"ID_pred/Softmax:0"}); desc.add>( "flav_names", std::vector{"probb", "probbb", "problepb", "probc", "probuds", "probg"}); + desc.add("onnx_backend", "default"); descriptions.add("pfDeepFlavourJetTags", desc); } std::unique_ptr DeepFlavourONNXJetTagsProducer::initializeGlobalCache(const edm::ParameterSet& iConfig) { - return std::make_unique(iConfig.getParameter("model_path").fullPath()); + auto session_options = cms::Ort::getSessionOptions(iConfig.getParameter("onnx_backend")); + return std::make_unique(iConfig.getParameter("model_path").fullPath(), + &session_options); } void DeepFlavourONNXJetTagsProducer::globalEndJob(const ONNXRuntime* cache) {} diff --git a/RecoBTag/ONNXRuntime/plugins/DeepVertexONNXJetTagsProducer.cc b/RecoBTag/ONNXRuntime/plugins/DeepVertexONNXJetTagsProducer.cc index c9805e66f04f7..60b975d33fbc9 100644 --- a/RecoBTag/ONNXRuntime/plugins/DeepVertexONNXJetTagsProducer.cc +++ b/RecoBTag/ONNXRuntime/plugins/DeepVertexONNXJetTagsProducer.cc @@ -14,6 +14,7 @@ #include "DataFormats/BTauReco/interface/DeepFlavourTagInfo.h" #include "PhysicsTools/ONNXRuntime/interface/ONNXRuntime.h" +#include "PhysicsTools/ONNXRuntime/interface/ONNXSessionOptions.h" #include "RecoBTag/ONNXRuntime/interface/tensor_fillers.h" #include "RecoBTag/ONNXRuntime/interface/tensor_configs.h" @@ -111,12 +112,15 @@ void DeepVertexONNXJetTagsProducer::fillDescriptions(edm::ConfigurationDescripti desc.add>("flav_names", std::vector{"probb", "probc", "probuds", "probg"}); desc.add("min_jet_pt", 15.0); desc.add("max_jet_eta", 2.5); + desc.add("onnx_backend", "default"); descriptions.add("pfDeepVertexJetTags", desc); } std::unique_ptr DeepVertexONNXJetTagsProducer::initializeGlobalCache(const edm::ParameterSet& iConfig) { - return std::make_unique(iConfig.getParameter("model_path").fullPath()); + auto session_options = cms::Ort::getSessionOptions(iConfig.getParameter("onnx_backend")); + return std::make_unique(iConfig.getParameter("model_path").fullPath(), + &session_options); } void DeepVertexONNXJetTagsProducer::globalEndJob(const ONNXRuntime* cache) {} diff --git a/RecoParticleFlow/PFProducer/plugins/MLPFProducer.cc b/RecoParticleFlow/PFProducer/plugins/MLPFProducer.cc index afd449922898a..cc791e464d251 100644 --- a/RecoParticleFlow/PFProducer/plugins/MLPFProducer.cc +++ b/RecoParticleFlow/PFProducer/plugins/MLPFProducer.cc @@ -5,6 +5,7 @@ #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h" #include "PhysicsTools/ONNXRuntime/interface/ONNXRuntime.h" +#include "PhysicsTools/ONNXRuntime/interface/ONNXSessionOptions.h" #include "RecoParticleFlow/PFProducer/interface/MLPFModel.h" #include "DataFormats/ParticleFlowReco/interface/PFBlockElementTrack.h" @@ -160,7 +161,8 @@ void MLPFProducer::produce(edm::Event& event, const edm::EventSetup& setup) { } std::unique_ptr MLPFProducer::initializeGlobalCache(const edm::ParameterSet& params) { - return std::make_unique(params.getParameter("model_path").fullPath()); + auto session_options = cms::Ort::getSessionOptions(params.getParameter("onnx_backend")); + return std::make_unique(params.getParameter("model_path").fullPath(), &session_options); } void MLPFProducer::globalEndJob(const ONNXRuntime* cache) {} @@ -173,6 +175,7 @@ void MLPFProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions edm::FileInPath( "RecoParticleFlow/PFProducer/data/mlpf/" "mlpf_2021_11_16__no_einsum__all_data_cms-best-of-asha-scikit_20211026_042043_178263.workergpu010.onnx")); + desc.add("onnx_backend", "default"); descriptions.addWithDefaultLabel(desc); }