Skip to content

Commit 82913de

Browse files
authored
Merge pull request #3 from uhh-cms/aot_support
Add AOT support
2 parents 54ba404 + b93db49 commit 82913de

File tree

122 files changed

+21479
-533
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+21479
-533
lines changed

.flake8

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[flake8]
22

3-
# line length of 100 is recommended, but set it to a forgiving value
3+
# line length
44
max-line-length = 120
55

66
# codes of errors to ignore
7-
ignore = E128, E306, E402, E722, E731, W504
7+
ignore = E128, E306, E402, E702, E722, E731, W504
88

99
# enforce double quotes
1010
inline-quotes = double

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
*.pb filter=lfs diff=lfs merge=lfs -text
77
*.pb.txt filter=lfs diff=lfs merge=lfs -text
88
*.pbtxt filter=lfs diff=lfs merge=lfs -text
9+
*.index filter=lfs diff=lfs merge=lfs -text
10+
*.data-* filter=lfs diff=lfs merge=lfs -text

.github/workflows/lint.yml

Lines changed: 0 additions & 27 deletions
This file was deleted.

.github/workflows/lint_and_test.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Lint and test
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
7+
jobs:
8+
lint:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout ⬇️
12+
uses: actions/checkout@v3
13+
with:
14+
persist-credentials: false
15+
16+
- name: Setup python 🐍
17+
uses: actions/setup-python@v4
18+
with:
19+
python-version: 3.9
20+
21+
- name: Install dependencies ☕️
22+
run: |
23+
pip install -U pip setuptools wheel
24+
pip install -r sandboxes/dev.txt
25+
26+
- name: Lint 🔍
27+
run: flake8 mlprof
28+
29+
typecheck:
30+
runs-on: ubuntu-latest
31+
steps:
32+
- name: Checkout ⬇️
33+
uses: actions/checkout@v3
34+
with:
35+
persist-credentials: false
36+
37+
- name: Setup python 🐍
38+
uses: actions/setup-python@v4
39+
with:
40+
python-version: 3.9
41+
42+
- name: Install dependencies ☕️
43+
run: |
44+
pip install -U pip setuptools wheel
45+
pip install -r sandboxes/dev.txt
46+
47+
- name: Typecheck 🥊
48+
run: mypy mlprof

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
*.npz
1313
*.h5
1414
*.hdf5
15-
*.json
16-
*.yaml
1715
*.out
1816
*.parquet
17+
*.vscode
1918
.coverage
2019
coverage*.xml
2120
__pycache__
@@ -28,3 +27,4 @@ software
2827
data
2928
.data
3029
.law
30+
.python-version

cmssw/MLProf/RuntimeMeasurement/plugins/ONNXInference.cc

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class ONNXInference : public edm::stream::EDAnalyzer<edm::GlobalCache<ONNXRuntim
3131
static void globalEndJob(const ONNXRuntime*);
3232

3333
private:
34-
void beginJob();
34+
void beginJob(){};
3535
void analyze(const edm::Event&, const edm::EventSetup&);
36-
void endJob();
36+
void endJob(){};
3737

3838
inline float drawNormal() { return normalPdf_(rndGen_); }
3939

@@ -76,10 +76,9 @@ void ONNXInference::fillDescriptions(edm::ConfigurationDescriptions& description
7676
// the rank (number of dimensions) of each input tensor
7777
desc.add<std::vector<int>>("inputRanks");
7878
// flat list of sizes of each dimension of each input tensor
79-
// (for a graph with a 1D and a 2D input tensor, this would be a vector of
80-
// three values)
79+
// (for a graph with a 1D and a 2D input tensor, this would be a vector of three values)
8180
desc.add<std::vector<int>>("flatInputSizes");
82-
// batch sizes to test
81+
// batch size to test
8382
desc.add<int>("batchSize");
8483
// the number of calls to the graph to measure the runtime
8584
desc.add<int>("nCalls");
@@ -133,9 +132,10 @@ ONNXInference::ONNXInference(const edm::ParameterSet& iConfig, const ONNXRuntime
133132
inputType_ = mlprof::InputType::Random;
134133
} else if (inputTypeStr_ == "zeros") {
135134
inputType_ = mlprof::InputType::Zeros;
135+
} else if (inputTypeStr_ == "ones") {
136+
inputType_ = mlprof::InputType::Ones;
136137
} else {
137-
throw cms::Exception("InvalidInputType")
138-
<< "input type must be either 'incremental', 'zeros' or 'random', got " << inputTypeStr_;
138+
throw cms::Exception("InvalidInputType") << "input type unknown: " << inputTypeStr_;
139139
}
140140

141141
// initialize the input_shapes array with inputRanks_ and flatInputSizes_
@@ -147,10 +147,12 @@ ONNXInference::ONNXInference(const edm::ParameterSet& iConfig, const ONNXRuntime
147147
i += rank;
148148
}
149149
// initialize the input data arrays
150-
// note there is only one element in the FloatArrays type (i.e.
151-
// vector<vector<float>>) variable
150+
// note there is only one element in the FloatArrays type (i.e. vector<vector<float>>) variable
152151
for (int i = 0; i < nInputs_; i++) {
153-
inputArrays_.emplace_back(batchSize_ * flatInputSizes_[i], 0);
152+
// multiply the size of all dimensions in an input
153+
int full_size_input = std::accumulate(begin(input_shapes_[i]), end(input_shapes_[i]), 1, std::multiplies<int>());
154+
// initialize inputArrays_ with 0s at first
155+
inputArrays_.emplace_back(full_size_input, 0);
154156
}
155157
}
156158

@@ -167,7 +169,7 @@ void ONNXInference::analyze(const edm::Event& iEvent, const edm::EventSetup& iSe
167169
for (int i = 0; i < (int)group_data.size(); i++) {
168170
group_data[i] = inputType_ == mlprof::InputType::Incremental
169171
? float(i)
170-
: (inputType_ == mlprof::InputType::Zeros ? float(0) : drawNormal());
172+
: float(inputType_ == mlprof::InputType::Zeros ? 0 : drawNormal());
171173
}
172174
}
173175

@@ -183,7 +185,10 @@ void ONNXInference::analyze(const edm::Event& iEvent, const edm::EventSetup& iSe
183185
std::vector<float> runtimes;
184186
for (int r = 0; r < nCalls_; r++) {
185187
auto start = std::chrono::high_resolution_clock::now();
188+
189+
// inference
186190
outputs = globalCache()->run(inputTensorNames_, inputArrays_, input_shapes_, outputTensorNames_, batchSize_);
191+
187192
auto end = std::chrono::high_resolution_clock::now();
188193
std::chrono::duration<float> runtime_in_seconds = (end - start);
189194
runtimes.push_back(runtime_in_seconds.count() * 1000);

cmssw/MLProf/RuntimeMeasurement/plugins/TFInference.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class TFInference : public edm::stream::EDAnalyzer<edm::GlobalCache<tensorflow::
2929
static void globalEndJob(const tensorflow::SessionCache*);
3030

3131
private:
32-
void beginJob();
32+
void beginJob(){};
3333
void analyze(const edm::Event&, const edm::EventSetup&);
34-
void endJob();
34+
void endJob(){};
3535

3636
inline float drawNormal() { return normalPdf_(rndGen_); }
3737
tensorflow::Tensor createInputTensor(int rank, std::vector<int> shape);
@@ -83,7 +83,7 @@ void TFInference::fillDescriptions(edm::ConfigurationDescriptions& descriptions)
8383
// flat list of sizes of each dimension of each input tensor
8484
// (for a graph with a 1D and a 2D input tensor, this would be a vector of three values)
8585
desc.add<std::vector<int>>("flatInputSizes");
86-
// batch sizes to test
86+
// batch size to test
8787
desc.add<int>("batchSize");
8888
// the number of calls to the graph to measure the runtime
8989
desc.add<int>("nCalls");
@@ -137,16 +137,13 @@ TFInference::TFInference(const edm::ParameterSet& config, const tensorflow::Sess
137137
inputType_ = mlprof::InputType::Random;
138138
} else if (inputTypeStr_ == "zeros") {
139139
inputType_ = mlprof::InputType::Zeros;
140+
} else if (inputTypeStr_ == "ones") {
141+
inputType_ = mlprof::InputType::Ones;
140142
} else {
141-
throw cms::Exception("InvalidInputType")
142-
<< "input type must be either 'incremental', 'zeros' or 'random', got " << inputTypeStr_;
143+
throw cms::Exception("InvalidInputType") << "input type unknown: " << inputTypeStr_;
143144
}
144145
}
145146

146-
void TFInference::beginJob() {}
147-
148-
void TFInference::endJob() {}
149-
150147
tensorflow::Tensor TFInference::createInputTensor(int rank, std::vector<int> shape) {
151148
// convert the shape to a tf shape
152149
tensorflow::TensorShape tShape;
@@ -162,7 +159,7 @@ tensorflow::Tensor TFInference::createInputTensor(int rank, std::vector<int> sha
162159
for (int i = 0; i < tensor.NumElements(); i++, data++) {
163160
*data = inputType_ == mlprof::InputType::Incremental
164161
? float(i)
165-
: (inputType_ == mlprof::InputType::Zeros ? float(0) : drawNormal());
162+
: float(inputType_ == mlprof::InputType::Zeros ? 0 : drawNormal());
166163
}
167164

168165
return tensor;
@@ -194,7 +191,10 @@ void TFInference::analyze(const edm::Event& event, const edm::EventSetup& setup)
194191
std::vector<float> runtimes;
195192
for (int r = 0; r < nCalls_; r++) {
196193
auto start = std::chrono::high_resolution_clock::now();
194+
195+
// inference
197196
tensorflow::run(session_, inputs, outputTensorNames_, &outputs);
197+
198198
auto end = std::chrono::high_resolution_clock::now();
199199
std::chrono::duration<float> runtime_in_seconds = (end - start);
200200
runtimes.push_back(runtime_in_seconds.count() * 1000);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<library name="MLProfRuntimeMeasurementTFAOTInference" file="TFAOTInference.cc">
2+
<use name="FWCore/Framework"/>
3+
<use name="FWCore/PluginManager"/>
4+
<use name="FWCore/ParameterSet"/>
5+
6+
<use name="PhysicsTools/TensorFlowAOT"/>
7+
<use name="MLProf/Utils"/>
8+
9+
<use name="tfaot-model-mlprof-test"/>
10+
11+
<flags EDM_PLUGIN="1"/>
12+
</library>

0 commit comments

Comments
 (0)