Skip to content

Commit

Permalink
Merge pull request #74 from cms-ml/tf_aot_docs
Browse files Browse the repository at this point in the history
TF AOT documentation.
  • Loading branch information
valsdav authored Apr 29, 2024
2 parents 33d0789 + d380f49 commit cebbd7b
Show file tree
Hide file tree
Showing 6 changed files with 652 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<use name="FWCore/Framework" />
<use name="FWCore/PluginManager" />
<use name="FWCore/ParameterSet" />

<use name="PhysicsTools/TensorFlowAOT" />
<use name="tfaot-model-test"/>

<flags EDM_PLUGIN="1" />
47 changes: 47 additions & 0 deletions content/inference/code/tensorflow_aot/tensorflow_aot_cfg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# coding: utf-8

import os

import FWCore.ParameterSet.Config as cms
from FWCore.ParameterSet.VarParsing import VarParsing


# get the data/ directory
thisdir = os.path.dirname(os.path.abspath(__file__))
datadir = os.path.join(os.path.dirname(thisdir), "data")

# setup minimal options
options = VarParsing("python")
options.setDefault("inputFiles", "root://xrootd-cms.infn.it//store/mc/RunIISummer20UL18MiniAODv2/GluGluToHHTo2G2Tau_node_cHHH1_TuneCP5_13TeV-powheg-pythia8/MINIAODSIM/106X_upgrade2018_realistic_v16_L1v1-v2/40000/871B714B-0AA3-3342-B56C-6DC0E634593A.root") # noqa
options.parseArguments()

# define the process to run
process = cms.Process("TEST")

# minimal configuration
process.load("FWCore.MessageService.MessageLogger_cfi")
process.MessageLogger.cerr.FwkReport.reportEvery = 1
process.maxEvents = cms.untracked.PSet(
input=cms.untracked.int32(10),
)
process.source = cms.Source(
"PoolSource",
fileNames=cms.untracked.vstring(options.inputFiles),
)

# process options
process.options = cms.untracked.PSet(
allowUnscheduled=cms.untracked.bool(True),
wantSummary=cms.untracked.bool(True),
)

# setup MyPlugin by loading the auto-generated cfi (see MyPlugin.fillDescriptions)
process.load("MySubsystem.MyModule.myPlugin_cfi")
# register three batch rules
# - add 1+1+1 for batch size 3
# - add 4+1 for batch size 5
# - add 4+4 for batch size 6, using a padding of 2 for the second call
process.myPlugin.batchRules = cms.vstring(["3:1,1,1", "5:4,1", "6:4,4"])

# define what to run in the path
process.p = cms.Path(process.myPlugin)
69 changes: 69 additions & 0 deletions content/inference/code/tensorflow_aot/tensorflow_aot_plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Example plugin to demonstrate the inference with TensorFlow AOT.
*/

#include <memory>

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/stream/EDAnalyzer.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "PhysicsTools/TensorFlowAOT/interface/Model.h"

// include the header of the compiled model
#include "tfaot-model-test/model.h"

class MyPlugin : public edm::stream::EDAnalyzer<> {
public:
explicit MyPlugin(const edm::ParameterSet&);
~MyPlugin(){};

static void fillDescriptions(edm::ConfigurationDescriptions&);

private:
void beginJob(){};
void analyze(const edm::Event&, const edm::EventSetup&);
void endJob(){};

std::vector<std::string> batchRuleStrings_;

// aot model
tfaot::Model<tfaot_model::test> model_;
};

void MyPlugin::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
// defining this function will lead to a *_cfi file being generated when compiling
edm::ParameterSetDescription desc;
desc.add<std::vector<std::string>>("batchRules");
descriptions.addWithDefaultLabel(desc);
}

MyPlugin::MyPlugin(const edm::ParameterSet& config)
: batchRuleStrings_(config.getParameter<std::vector<std::string>>("batchRules")) {
// register batch rules
for (const auto& rule : batchRuleStrings_) {
model_.setBatchRule(rule);
}
}

void MyPlugin::analyze(const edm::Event& event, const edm::EventSetup& setup) {
// define input for a batch size of 1
// (just a single float input, with shape 1x4)
tfaot::FloatArrays input = { {0, 1, 2, 3} };

// define output
// (just a single float output, which will shape 1x2)
tfaot::FloatArrays output;

// evaluate the model
// the template arguments of run() correspond to the types of the outputs
// that are "tied" the "1" denote the batch size of 1
std::tie(output) = model_.run<tfaot::FloatArrays>(1, input);

// print output
std::cout << "output[0]: " << output[0][0] << ", " << output[0][1] << std::endl;
// -> "output[0]: 0.648093, 0.351907"
}

DEFINE_FWK_MODULE(MyPlugin);
Loading

0 comments on commit cebbd7b

Please sign in to comment.