Skip to content

Commit 6d9c75f

Browse files
committed
add support for writing output result for debugging
1 parent 9cb47ca commit 6d9c75f

File tree

2 files changed

+75
-13
lines changed

2 files changed

+75
-13
lines changed

root/tmva/sofie/ONNXRuntimeInference_Template.cxx.in

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
#include <numeric>
1111
#include <random>
1212
#include <chrono>
13+
#include <fstream>
1314

1415
using namespace std;
1516

17+
bool testOutput = true;
18+
1619
static void @FUNC_NAME@(benchmark::State& state, string model_path)
1720
{
1821
Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "benchmark");
@@ -77,10 +80,14 @@ static void @FUNC_NAME@(benchmark::State& state, string model_path)
7780
// std::cout << "input tensor size " << input_tensor_size << " " << input_tensor_values.size() << std::endl;
7881

7982
// Input tensor initialization
80-
static std::uniform_real_distribution<float> distribution(-1, 1);
81-
static std::default_random_engine generator;
82-
std::generate(input_tensor_values.begin(), input_tensor_values.end(), []() { return distribution(generator); });
83-
// fill_n(input_tensor_values.begin(), input_tensor_size, 1.0);
83+
84+
if (testOutput)
85+
fill_n(input_tensor_values.begin(), input_tensor_values.size(), float(i)+1.);
86+
else {
87+
static std::uniform_real_distribution<float> distribution(-1, 1);
88+
static std::default_random_engine generator;
89+
std::generate(input_tensor_values.begin(), input_tensor_values.end(), []() { return distribution(generator); });
90+
}
8491
}
8592

8693
auto memory_info = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
@@ -93,6 +100,12 @@ static void @FUNC_NAME@(benchmark::State& state, string model_path)
93100

94101
std::vector<Ort::Value> input_tensors;
95102

103+
size_t osize = 1;
104+
for (int d : output_node_dims[0]) {
105+
if (d > 0) osize *= d; // first dim(batch size) can be -1
106+
}
107+
std::vector<float> yOut(osize);
108+
96109
double totDuration = 0;
97110
int ntimes = 0;
98111
for (auto _ : state) {
@@ -113,12 +126,25 @@ static void @FUNC_NAME@(benchmark::State& state, string model_path)
113126
for (int k = 0; k < nin; k++) {
114127
input_offset[k] += inputSizes[k];
115128
}
129+
if (testOutput && i == 0)
130+
std::copy(floatarr, floatarr + osize, yOut.begin());
116131
}
117132

118133
auto t2 = std::chrono::high_resolution_clock::now();
119134
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
120135
totDuration += duration / 1.E3; // in milliseconds
121136
ntimes++;
137+
if (testOutput) {
138+
std::string filename = model_path + ".ort.out";
139+
//std::cout << "writing file" << filename << std::endl;
140+
ofstream f;
141+
f.open(filename);
142+
f << yOut.size() << std::endl;
143+
for (size_t i = 0; i < yOut.size(); i++)
144+
f << yOut[i] << " ";
145+
f << std::endl;
146+
f.close();
147+
}
122148
}
123149
//for (int i = 0; i < 10; i++)
124150
// printf("%f\t", i, floatarr[i]);

root/tmva/sofie/SOFIEInference.cxx

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,62 @@
3434

3535
using namespace std;
3636
bool verbose = false;
37+
bool testOutput = true;
38+
39+
3740
template <class S>
3841
void BM_SOFIE_Inference(benchmark::State &state)
3942
{
4043
size_t inputSize = state.range(0); // input size (without batch size)
41-
size_t bsize = (state.range(1) > 0) ? state.range(1) : 0;
44+
size_t bsize = (state.range(1) > 0) ? state.range(1) : 1;
4245
size_t nevts = 64;
4346
size_t nrep = nevts / bsize;
4447

4548
vector<float> input(inputSize*nevts);
4649

47-
static std::uniform_real_distribution<float> distribution(-1, 1);
48-
static std::default_random_engine generator;
49-
std::generate(input.begin(), input.end(), []() { return distribution(generator); });
50-
50+
if (testOutput) {
51+
input = std::vector<float>(input.size(),1.);
52+
}
53+
else {
54+
static std::uniform_real_distribution<float> distribution(-1, 1);
55+
static std::default_random_engine generator;
56+
std::generate(input.begin(), input.end(), []() { return distribution(generator); });
57+
}
5158
float *input_ptr = input.data();
5259
S s("");
5360

5461
double totDuration = 0;
5562
int ntimes = 0;
63+
std::vector<float> yOut;
64+
bool first = true;
65+
bool doWrite = testOutput;
5666
for (auto _ : state) {
5767
auto t1 = std::chrono::high_resolution_clock::now();
58-
for (int i = 0; i < nevts; i += bsize)
68+
for (int i = 0; i < nevts; i += bsize) {
5969
auto y = s.infer(input.data()+ inputSize*i);
60-
70+
if (first) {
71+
//std::cout << std::string(typeid(s).name()) << " : " << y[0] << " " << y[1] << std::endl;
72+
yOut = y;
73+
first = false;
74+
}
75+
}
6176
auto t2 = std::chrono::high_resolution_clock::now();
6277
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
6378
totDuration += duration / 1.E3; // in milliseconds
6479
ntimes++;
80+
if (doWrite) {
81+
// write output for test
82+
//std::cout << "write output " << std::endl;
83+
std::ofstream f;
84+
std::string filename = std::string(typeid(s).name()) + ".out";
85+
f.open(filename);
86+
f << yOut.size() << std::endl;
87+
for (size_t i = 0; i < yOut.size(); i++)
88+
f << yOut[i] << " ";
89+
f << std::endl;
90+
f.close();
91+
doWrite = false;
92+
}
6593
}
6694

6795
state.counters["time/evt(ms)"] = totDuration / double(ntimes * nevts);
@@ -95,11 +123,19 @@ void BM_SOFIE_Inference_3(benchmark::State &state)
95123
vector<float> input2(inputSize2*nevts);
96124
vector<float> input3(inputSize3*nevts);
97125

126+
if (!testOutput) {
98127
static std::uniform_real_distribution<float> distribution(-1, 1);
99128
static std::default_random_engine generator;
100129
std::generate(input1.begin(), input1.end(), []() { return distribution(generator); });
101130
std::generate(input2.begin(), input2.end(), []() { return distribution(generator); });
102131
std::generate(input3.begin(), input3.end(), []() { return distribution(generator); });
132+
}
133+
else {
134+
// generate fixed data
135+
input1 = vector<float>(input1.size(),1.);
136+
input2 = vector<float>(input2.size(),2.);
137+
input3 = vector<float>(input3.size(),3.);
138+
}
103139

104140
S s("");
105141

@@ -125,9 +161,9 @@ void BM_SOFIE_Inference_3(benchmark::State &state)
125161
}
126162

127163
// CMS benchmark (3 inputs)
128-
BENCHMARK_TEMPLATE(BM_SOFIE_Inference_3, TMVA_SOFIE_DDB_B1::Session)->Name("DDB_B1")->Args({1, 1*27, 60*8, 5*2})->Unit(benchmark::kMillisecond);
164+
//BENCHMARK_TEMPLATE(BM_SOFIE_Inference_3, TMVA_SOFIE_DDB_B1::Session)->Name("DDB_B1")->Args({1, 1*27, 60*8, 5*2})->Unit(benchmark::kMillisecond);
129165
// Conv Transpose
130-
BENCHMARK_TEMPLATE(BM_SOFIE_Inference, TMVA_SOFIE_Conv2DTranspose_Relu_Sigmoid::Session)->Name("Cov2DTranspose_B1")->Args({1, 1*15})->Unit(benchmark::kMillisecond);
166+
BENCHMARK_TEMPLATE(BM_SOFIE_Inference, TMVA_SOFIE_Conv2DTranspose_Relu_Sigmoid::Session)->Name("Cov2DTranspose_Relu_Sigmoid")->Args({15,1})->Unit(benchmark::kMillisecond);
131167

132168
//Gemm benchmarks
133169
BENCHMARK_TEMPLATE(BM_SOFIE_Inference, TMVA_SOFIE_Linear_16::Session)->Name("Linear_16")->Args({100, 16})->Unit(benchmark::kMillisecond);

0 commit comments

Comments
 (0)