1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
//
// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//
#include "TestBlocks.hpp"
#include "TestStrategy.hpp"
#include <IMemoryOptimizerStrategy.hpp>
#include <MemoryOptimizerStrategyLibrary.hpp>
#include <strategies/StrategyValidator.hpp>
#include <cxxopts.hpp>
#include <iostream>
#include <algorithm>
#include <iomanip>
std::vector<TestBlock> testBlocks
{
{"fsrcnn", fsrcnn},
{"inceptionv4", inceptionv4},
{"deeplabv3", deeplabv3},
{"deepspeechv1", deepspeechv1},
{"mobilebert", mobilebert},
{"ssd_mobilenetv2", ssd_mobilenetv2},
{"resnetv2", resnetv2},
{"yolov3",yolov3}
};
void PrintModels()
{
std::cout << "Available models:\n";
for (const auto& model : testBlocks)
{
std::cout << model.m_Name << "\n";
}
std::cout << "\n";
}
size_t GetMinPossibleMemorySize(const std::vector<armnn::MemBlock>& blocks)
{
unsigned int maxLifetime = 0;
for (auto& block: blocks)
{
maxLifetime = std::max(maxLifetime, block.m_EndOfLife);
}
maxLifetime++;
std::vector<size_t> lifetimes(maxLifetime);
for (const auto& block : blocks)
{
for (auto lifetime = block.m_StartOfLife; lifetime <= block.m_EndOfLife; ++lifetime)
{
lifetimes[lifetime] += block.m_MemSize;
}
}
return *std::max_element(lifetimes.begin(), lifetimes.end());
}
void RunBenchmark(armnn::IMemoryOptimizerStrategy* strategy, std::vector<TestBlock>* models)
{
using Clock = std::chrono::high_resolution_clock;
float avgEfficiency = 0;
std::chrono::duration<double, std::milli> avgDuration{};
std::cout << "\nMemory Strategy: " << strategy->GetName()<< "\n";
std::cout << "===============================================\n";
for (auto& model : *models)
{
auto now = Clock::now();
const std::vector<armnn::MemBin> result = strategy->Optimize(model.m_Blocks);
auto duration = std::chrono::duration<double, std::milli>(Clock::now() - now);
avgDuration += duration;
size_t memoryUsage = 0;
for (auto bin : result)
{
memoryUsage += bin.m_MemSize;
}
size_t minSize = GetMinPossibleMemorySize(model.m_Blocks);
float efficiency = static_cast<float>(minSize) / static_cast<float>(memoryUsage);
efficiency*=100;
avgEfficiency += efficiency;
std::cout << "\nModel: " << model.m_Name << "\n";
std::cout << "Strategy execution time: " << std::setprecision(4) << duration.count() << " milliseconds\n";
std::cout << "Memory usage: " << memoryUsage/1024 << " kb\n";
std::cout << "Minimum possible usage: " << minSize/1024 << " kb\n";
std::cout << "Memory efficiency: " << std::setprecision(3) << efficiency << "%\n";
}
avgDuration/= static_cast<double>(models->size());
avgEfficiency/= static_cast<float>(models->size());
std::cout << "\n===============================================\n";
std::cout << "Average memory duration: " << std::setprecision(4) << avgDuration.count() << " milliseconds\n";
std::cout << "Average memory efficiency: " << std::setprecision(3) << avgEfficiency << "%\n";
}
struct BenchmarkOptions
{
std::string m_StrategyName;
std::string m_ModelName;
bool m_UseDefaultStrategy = false;
bool m_Validate = false;
};
BenchmarkOptions ParseOptions(int argc, char* argv[])
{
cxxopts::Options options("Memory Benchmark", "Tests memory optimization strategies on different models");
options.add_options()
("s, strategy", "Strategy name, do not specify to use default strategy", cxxopts::value<std::string>())
("m, model", "Model name", cxxopts::value<std::string>())
("v, validate", "Validate strategy", cxxopts::value<bool>()->default_value("false")->implicit_value("true"))
("h,help", "Display usage information");
auto result = options.parse(argc, argv);
if (result.count("help"))
{
std::cout << options.help() << std::endl;
PrintModels();
std::cout << "\nAvailable strategies:\n";
for (const auto& s :armnn::GetMemoryOptimizerStrategyNames())
{
std::cout << s << "\n";
}
exit(EXIT_SUCCESS);
}
BenchmarkOptions benchmarkOptions;
if(result.count("strategy"))
{
benchmarkOptions.m_StrategyName = result["strategy"].as<std::string>();
}
else
{
std::cout << "No Strategy given, using default strategy";
benchmarkOptions.m_UseDefaultStrategy = true;
}
if(result.count("model"))
{
benchmarkOptions.m_ModelName = result["model"].as<std::string>();
}
benchmarkOptions.m_Validate = result["validate"].as<bool>();
return benchmarkOptions;
}
int main(int argc, char* argv[])
{
BenchmarkOptions benchmarkOptions = ParseOptions(argc, argv);
std::shared_ptr<armnn::IMemoryOptimizerStrategy> strategy;
if (benchmarkOptions.m_UseDefaultStrategy)
{
strategy = std::make_shared<armnn::TestStrategy>();
}
else
{
strategy = armnn::GetMemoryOptimizerStrategy(benchmarkOptions.m_StrategyName);
if (!strategy)
{
std::cout << "Strategy name not found\n";
return 0;
}
}
std::vector<TestBlock> model;
std::vector<TestBlock>* modelsToTest = &testBlocks;
if (benchmarkOptions.m_ModelName.size() != 0)
{
auto it = std::find_if(testBlocks.cbegin(), testBlocks.cend(), [&](const TestBlock testBlock)
{
return testBlock.m_Name == benchmarkOptions.m_ModelName;
});
if (it == testBlocks.end())
{
std::cout << "Model name not found\n";
return 0;
}
else
{
model.push_back(*it);
modelsToTest = &model;
}
}
if (benchmarkOptions.m_Validate)
{
armnn::StrategyValidator strategyValidator;
strategyValidator.SetStrategy(strategy);
RunBenchmark(&strategyValidator, modelsToTest);
}
else
{
RunBenchmark(strategy.get(), modelsToTest);
}
}
|