-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Extend onnxruntime gpu interface to producers using onnxruntime #39402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davidlange6
wants to merge
7
commits into
cms-sw:master
Choose a base branch
from
davidlange6:dl220915t
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dd6f102
add small helper to determine onnx backend
davidlange6 6e9f925
add small helper to determine onnx backend
davidlange6 defc129
if gpu present and active use it unless overridden
davidlange6 28bcc71
add hooks for debugging; reduce optimizations for one model that requ…
davidlange6 9433eda
code checks
davidlange6 68fc2b3
Update PhysicsTools/ONNXRuntime/interface/ONNXSessionOptions.h
davidlange6 54d730f
Update BuildFile.xml
davidlange6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
<use name="onnxruntime"/> | ||
<use name="FWCore/Utilities"/> | ||
<use name="HeterogeneousCore/CUDAServices"/> | ||
<use name="FWCore/ServiceRegistry"/> | ||
<export> | ||
<lib name="1"/> | ||
</export> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <string> | ||
|
||
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<CUDAService> cs; | ||
if (cs.isAvailable() and cs->enabled()) { | ||
backend = cms::Ort::Backend::cuda; | ||
} | ||
} | ||
|
||
return ONNXRuntime::defaultSessionOptions(backend); | ||
} | ||
} // namespace cms::Ort | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<std::vector<std::string>>("flav_names", std::vector<std::string>{"probb", "probc", "probuds", "probg"}); | ||
desc.add<double>("min_jet_pt", 15.0); | ||
desc.add<double>("max_jet_eta", 2.5); | ||
desc.add<std::string>("onnx_backend", "default"); | ||
|
||
descriptions.add("pfDeepCombinedJetTags", desc); | ||
} | ||
|
||
std::unique_ptr<ONNXRuntime> DeepCombinedONNXJetTagsProducer::initializeGlobalCache(const edm::ParameterSet& iConfig) { | ||
return std::make_unique<ONNXRuntime>(iConfig.getParameter<edm::FileInPath>("model_path").fullPath()); | ||
auto session_options = cms::Ort::getSessionOptions(iConfig.getParameter<std::string>("onnx_backend")); | ||
return std::make_unique<ONNXRuntime>(iConfig.getParameter<edm::FileInPath>("model_path").fullPath(), | ||
&session_options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really for this PR, but I think the |
||
} | ||
|
||
void DeepCombinedONNXJetTagsProducer::globalEndJob(const ONNXRuntime* cache) {} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering what should happen if
process.options.accelerators = ["cpu"]
on a machine with a GPU and the job as an ONNX module explicitly configured to usecuda
. I suppose in general we'd want such a setup to not run any GPU code (this is what happens withSwitchProducerCUDA
and in all CUDA EDModules).If we want ONNX modules to also avoid using GPUs when
process.options.accelerators = ["cpu"]
, the case of explicitcuda
choice should also check if theCUDAService
is enabled, and throw an exception if it is not.(and in the longer term we should think of how to improve the mechanism)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah - great - I saw the issue at some point, but missed that it was fixed (as the issue was open...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidlange6 Here is the code snipped I mentioned in order to make the "
param_backend == "cuda"
case to fail the job for theprocess.options.accelerators = ["cpu"]
case(although I have a feeling there is room for future simplification in conjunction with the
default
case handling below)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How should we proceed here? Improve the logic now, or merge this PR and make a quick follow-up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I thought I had written this comment already earlier, but apparently not)
We could also make the code here less dependent on the availability of CUDA runtime in the CMSSW build by using
edm::ResourceInformation
service along(although we should probably think of better API in
edm::ResourceInformation
for this use case, e.g. the number of devices)