|
| 1 | +// Author: Federico Sossai (fsossai), 2021 |
| 2 | + |
| 3 | +#include <benchmark/benchmark.h> |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | +#include <thread> |
| 7 | +#include <chrono> |
| 8 | +#include <utility> |
| 9 | +#include <vector> |
| 10 | +#include <memory> |
| 11 | +#include <functional> |
| 12 | +#include <random> |
| 13 | +#include <fstream> |
| 14 | +#include <stdlib.h> |
| 15 | + |
| 16 | +#include "TMath.h" |
| 17 | + |
| 18 | +#include "lwtnn/LightweightNeuralNetwork.hh" |
| 19 | +#include "lwtnn/LightweightGraph.hh" |
| 20 | +#include "lwtnn/parse_json.hh" |
| 21 | + |
| 22 | +bool verbose = false; |
| 23 | + |
| 24 | +void BM_LWTNN_Inference_model(benchmark::State &state, std::string model_name, size_t inputSize, size_t outputSize = 1) |
| 25 | +{ |
| 26 | + |
| 27 | + |
| 28 | + std::map<std::string, double> inputs; |
| 29 | + std::vector<std::string> names; |
| 30 | + |
| 31 | + std::string model_filename = model_name + ".json"; |
| 32 | + std::ifstream config_file(model_filename); |
| 33 | + |
| 34 | + auto config = lwt::parse_json(config_file); |
| 35 | + |
| 36 | + // Set up neural network model from config |
| 37 | + auto model = std::make_unique<lwt::LightweightNeuralNetwork>(config.inputs, config.layers, config.outputs); |
| 38 | + |
| 39 | + config_file.close(); |
| 40 | + |
| 41 | + // Initialize input |
| 42 | + //std::cout << "input size is " << config.inputs.size() << std::endl; |
| 43 | + if (config.inputs.size() != inputSize ) { |
| 44 | + throw std::runtime_error("Bad input size - it is " + std::to_string(inputSize) + |
| 45 | + " and should be " + std::to_string(config.inputs.size())); |
| 46 | + } |
| 47 | + for (size_t n = 0; n < inputSize; n++) { |
| 48 | + inputs[config.inputs.at(n).name] = 0.0; |
| 49 | + names.push_back(config.inputs.at(n).name); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + // size_t inputSize = state.range(0); // input size (without batch size) |
| 54 | + size_t bsize = 1; // bsize is always 1 for lwtnn |
| 55 | + size_t nevts = 64; |
| 56 | + size_t nrep = nevts / bsize; |
| 57 | + |
| 58 | + std::vector<float> input(inputSize * nevts); |
| 59 | + |
| 60 | + static std::uniform_real_distribution<float> distribution(-1, 1); |
| 61 | + static std::default_random_engine generator; |
| 62 | + std::generate(input.begin(), input.end(), []() { return distribution(generator); }); |
| 63 | + |
| 64 | + double totDuration = 0; |
| 65 | + int ntimes = 0; |
| 66 | + std::vector<float> y(outputSize); |
| 67 | + for (auto _ : state) { |
| 68 | + auto t1 = std::chrono::high_resolution_clock::now(); |
| 69 | + for (size_t i = 0; i < nevts; i += bsize) { |
| 70 | + for (size_t j = 0; j < inputSize; j++) |
| 71 | + //inputs["node_0"]["variable_" + std::to_string(j)] = input[i * inputSize + j]; |
| 72 | + inputs[names[j]] = input[i * inputSize + j]; |
| 73 | + |
| 74 | + auto outputs = model->compute(inputs); |
| 75 | + y[0] = outputs.begin()->second; |
| 76 | + // for (int i = 0; i < outputSize; i++) |
| 77 | + // y[i] = outputs["out_" + std::to_string(i)]; |
| 78 | + } |
| 79 | + |
| 80 | + auto t2 = std::chrono::high_resolution_clock::now(); |
| 81 | + auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count(); |
| 82 | + totDuration += duration / 1.E3; // in milliseconds |
| 83 | + ntimes++; |
| 84 | + } |
| 85 | + |
| 86 | + state.counters["time/evt(ms)"] = totDuration / double(ntimes * nevts); |
| 87 | +} |
| 88 | + |
| 89 | +void BM_LWTNN_Inference_graph(benchmark::State &state, std::string model_name, size_t inputSize, size_t outputSize = 1 ) |
| 90 | +{ |
| 91 | + |
| 92 | + typedef std::map<std::string, std::map<std::string, double>> NetworkInputs; |
| 93 | + typedef std::map<std::string, double> NetworkOutputs; |
| 94 | + |
| 95 | + //std::map<std::string, double> inputs; |
| 96 | + NetworkInputs inputs; |
| 97 | + std::vector<std::string> names; |
| 98 | + |
| 99 | + std::string model_filename = model_name + ".json"; |
| 100 | + std::ifstream config_file(model_filename); |
| 101 | + auto graph = std::make_unique<lwt::LightweightGraph>(lwt::parse_json_graph(config_file)); |
| 102 | + config_file.close(); |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + //size_t inputSize = state.range(0); // input size (without batch size) |
| 107 | + size_t bsize = 1; // bsize is always 1 for lwtnn |
| 108 | + size_t nevts = 64; |
| 109 | + size_t nrep = nevts / bsize; |
| 110 | + |
| 111 | + std::vector<float> input(inputSize * nevts); |
| 112 | + |
| 113 | + static std::uniform_real_distribution<float> distribution(-1, 1); |
| 114 | + static std::default_random_engine generator; |
| 115 | + std::generate(input.begin(), input.end(), []() { return distribution(generator); }); |
| 116 | + |
| 117 | + |
| 118 | + double totDuration = 0; |
| 119 | + int ntimes = 0; |
| 120 | + std::vector<float> y(outputSize); |
| 121 | + for (auto _ : state) { |
| 122 | + auto t1 = std::chrono::high_resolution_clock::now(); |
| 123 | + for (size_t i = 0; i < nevts; i += bsize) { |
| 124 | + for (size_t j = 0; j < inputSize; j++) |
| 125 | + inputs["node_0"]["variable_" + std::to_string(j)] = input[i * inputSize + j]; |
| 126 | + // inputs[names[j]] = input[i * inputSize + j]; |
| 127 | + |
| 128 | + auto outputs = graph->compute(inputs); |
| 129 | + for (int i = 0; i < outputSize; i++) |
| 130 | + y[i] = outputs["out_" + std::to_string(i)]; |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + auto t2 = std::chrono::high_resolution_clock::now(); |
| 135 | + auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count(); |
| 136 | + totDuration += duration / 1.E3; // in milliseconds |
| 137 | + ntimes++; |
| 138 | + } |
| 139 | + |
| 140 | + state.counters["time/evt(ms)"] = totDuration / double(ntimes * nevts); |
| 141 | +} |
| 142 | + |
| 143 | +//LWTNN benchmarks |
| 144 | +// use B<_CAPTURE to pass string of file, second parameter is name of test and is arbitrary |
| 145 | +BENCHMARK_CAPTURE(BM_LWTNN_Inference_model,higgs_model_dense, "higgs_model_dense",7)->Unit(benchmark::kMillisecond); |
| 146 | +BENCHMARK_CAPTURE(BM_LWTNN_Inference_graph, generator, "Generator",14)->Unit(benchmark::kMillisecond); |
| 147 | + |
| 148 | +BENCHMARK_MAIN(); |
0 commit comments