Skip to content

Commit 640e8c6

Browse files
committed
Improve onnx app portability
1 parent 0fd801c commit 640e8c6

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

apps/onnx/model.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
#include "common_types.h"
77
#include "denormal_disabler.h"
88
#include "onnx_converter.h"
9+
#include <chrono>
910
#include <fstream>
1011
#include <random>
11-
#include <sys/time.h>
1212
#include <unordered_set>
1313

1414
namespace py = pybind11;
@@ -436,7 +436,7 @@ double benchmark(
436436
CacheEvictor cache_evictor;
437437

438438
// Generate random value for every input
439-
for (ssize_t i = 0; i < pipeline.model->inputs.size(); ++i) {
439+
for (size_t i = 0; i < pipeline.model->inputs.size(); ++i) {
440440
const std::string &input_name = pipeline.input_names[i];
441441
prepare_random_input(pipeline, input_name);
442442
}
@@ -469,29 +469,28 @@ double benchmark(
469469
pipeline.rep->realize(real, tgt);
470470

471471
// Now benchmark by computing the value of the outputs num_iter times
472-
struct timespec start;
473-
struct timespec end;
474-
clock_gettime(CLOCK_REALTIME, &start);
472+
using clock = std::chrono::high_resolution_clock;
473+
auto start = clock::now();
475474
for (int i = 0; i < num_iters; ++i) {
476475
// Increment the coefficients store in the cache evictor: this ensures that
477476
// all the data left in caches from the previous iteration is flushed out.
478477
cache_evictor.flush_caches();
479478
pipeline.rep->realize(real, tgt);
480479
}
481-
clock_gettime(CLOCK_REALTIME, &end);
480+
auto end = clock::now();
482481

483482
double total_runtime =
484-
(end.tv_sec - start.tv_sec) * 1e9 + end.tv_nsec - start.tv_nsec;
483+
std::chrono::duration<double, std::nano>(end - start).count();
485484

486485
// Figure out how long it took to generate new inputs at every iteration
487486
// and adjust the runtime accordingly.
488-
clock_gettime(CLOCK_REALTIME, &start);
487+
start = clock::now();
489488
for (int i = 0; i < num_iters; ++i) {
490489
cache_evictor.flush_caches();
491490
}
492-
clock_gettime(CLOCK_REALTIME, &end);
491+
end = clock::now();
493492
double input_gen_time =
494-
(end.tv_sec - start.tv_sec) * 1e9 + end.tv_nsec - start.tv_nsec;
493+
std::chrono::duration<double, std::nano>(end - start).count();
495494

496495
total_runtime -= input_gen_time;
497496

apps/onnx/onnx_converter.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#include "onnx_converter.h"
22
#include <climits>
3-
#include <exception>
4-
#include <math.h>
3+
#include <numeric>
54
#include <unordered_set>
65

6+
#define _USE_MATH_DEFINES
7+
#include <math.h>
8+
79
static Halide::Expr div_up(Halide::Expr num, int denom) {
810
return Halide::Internal::simplify((num + denom - 1) / denom);
911
}

0 commit comments

Comments
 (0)