提交 4b6dc13c 编写于 作者: L Liangliang He

Add model benchmark metrics

上级 db75d542
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
*/ */
#include <malloc.h> #include <malloc.h>
#include <stdint.h> #include <stdint.h>
#include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
...@@ -189,8 +190,8 @@ bool RunModel(const std::vector<std::string> &input_names, ...@@ -189,8 +190,8 @@ bool RunModel(const std::vector<std::string> &input_names,
mace::MACE_MODEL_TAG::LoadModelData(FLAGS_model_data_file.c_str()); mace::MACE_MODEL_TAG::LoadModelData(FLAGS_model_data_file.c_str());
NetDef net_def = mace::MACE_MODEL_TAG::CreateNet(model_data); NetDef net_def = mace::MACE_MODEL_TAG::CreateNet(model_data);
int64_t t1 = NowMicros(); int64_t t1 = NowMicros();
LOG(INFO) << "CreateNetDef latency: " << t1 - t0 << " us"; double create_net_millis = (t1 - t0) / 1000.0;
int64_t init_micros = t1 - t0; LOG(INFO) << "CreateNetDef latency: " << create_net_millis << " ms";
DeviceType device_type = ParseDeviceType(FLAGS_device); DeviceType device_type = ParseDeviceType(FLAGS_device);
LOG(INFO) << "Runing with device type: " << device_type; LOG(INFO) << "Runing with device type: " << device_type;
...@@ -207,15 +208,16 @@ bool RunModel(const std::vector<std::string> &input_names, ...@@ -207,15 +208,16 @@ bool RunModel(const std::vector<std::string> &input_names,
// Init model // Init model
LOG(INFO) << "Run init"; LOG(INFO) << "Run init";
t0 = NowMicros();
mace::MaceEngine engine(&net_def, device_type, input_names, output_names); mace::MaceEngine engine(&net_def, device_type, input_names, output_names);
if (device_type == DeviceType::OPENCL || device_type == DeviceType::HEXAGON) { if (device_type == DeviceType::OPENCL || device_type == DeviceType::HEXAGON) {
mace::MACE_MODEL_TAG::UnloadModelData(model_data); mace::MACE_MODEL_TAG::UnloadModelData(model_data);
} }
t1 = NowMicros(); int64_t t2 = NowMicros();
init_micros += t1 - t0; double mace_engine_ctor_millis = (t2 - t1) / 1000.0;
LOG(INFO) << "Net init latency: " << t1 - t0 << " us"; double init_millis = (t2 - t0) / 1000.0;
LOG(INFO) << "Total init latency: " << init_micros << " us"; LOG(INFO) << "MaceEngine constructor latency: "
<< mace_engine_ctor_millis << " ms";
LOG(INFO) << "Total init latency: " << init_millis << " ms";
const size_t input_count = input_names.size(); const size_t input_count = input_names.size();
const size_t output_count = output_names.size(); const size_t output_count = output_names.size();
...@@ -253,14 +255,16 @@ bool RunModel(const std::vector<std::string> &input_names, ...@@ -253,14 +255,16 @@ bool RunModel(const std::vector<std::string> &input_names,
} }
LOG(INFO) << "Warm up run"; LOG(INFO) << "Warm up run";
t0 = NowMicros(); int64_t t3 = NowMicros();
engine.Run(inputs, &outputs); engine.Run(inputs, &outputs);
t1 = NowMicros(); int64_t t4 = NowMicros();
LOG(INFO) << "1st warm up run latency: " << t1 - t0 << " us"; double warmup_millis = (t4 - t3) / 1000.0;
LOG(INFO) << "1st warm up run latency: " << warmup_millis << " ms";
double model_run_millis = -1;
if (FLAGS_round > 0) { if (FLAGS_round > 0) {
LOG(INFO) << "Run model"; LOG(INFO) << "Run model";
t0 = NowMicros(); int64_t t0 = NowMicros();
struct mallinfo prev = mallinfo(); struct mallinfo prev = mallinfo();
for (int i = 0; i < FLAGS_round; ++i) { for (int i = 0; i < FLAGS_round; ++i) {
engine.Run(inputs, &outputs); engine.Run(inputs, &outputs);
...@@ -269,10 +273,18 @@ bool RunModel(const std::vector<std::string> &input_names, ...@@ -269,10 +273,18 @@ bool RunModel(const std::vector<std::string> &input_names,
prev = LogMallinfoChange(prev); prev = LogMallinfoChange(prev);
} }
} }
t1 = NowMicros(); int64_t t1 = NowMicros();
LOG(INFO) << "Average latency: " << (t1 - t0) / FLAGS_round << " us"; model_run_millis = (t1 - t0) / 1000.0 / FLAGS_round;
LOG(INFO) << "Average latency: " << model_run_millis << " ms";
} }
// Metrics reporting tools depends on the format, keep in consistent
printf("================================================================\n");
printf(" create_net engine_ctor init warmup run_avg\n");
printf("================================================================\n");
printf("time %11.3f %11.3f %11.3f %11.3f %11.3f\n", create_net_millis,
mace_engine_ctor_millis, init_millis, warmup_millis, model_run_millis);
for (size_t i = 0; i < output_count; ++i) { for (size_t i = 0; i < output_count; ++i) {
std::string output_name = std::string output_name =
FLAGS_output_file + "_" + FormatName(output_names[i]); FLAGS_output_file + "_" + FormatName(output_names[i]);
......
...@@ -33,10 +33,16 @@ def ops_benchmark_stdout_processor(stdout, device_properties, abi): ...@@ -33,10 +33,16 @@ def ops_benchmark_stdout_processor(stdout, device_properties, abi):
line = line.strip() line = line.strip()
parts = line.split() parts = line.split()
if len(parts) == 5 and parts[0].startswith("BM_"): if len(parts) == 5 and parts[0].startswith("BM_"):
metrics["%s.time_ms" % parts[0]] = str(float(parts[1])/1000000.0) metrics["%s.time_ms" % parts[0]] = str(float(parts[1])/1e6)
metrics["%s.input_mb_per_sec" % parts[0]] = parts[3] metrics["%s.input_mb_per_sec" % parts[0]] = parts[3]
metrics["%s.gmacc_per_sec" % parts[0]] = parts[4] metrics["%s.gmacc_per_sec" % parts[0]] = parts[4]
sh_commands.falcon_push_metrics(metrics, device_properties, abi,
platform = device_properties["ro.board.platform"].replace(" ", "-")
model = device_properties["ro.product.model"].replace(" ", "-")
tags = {"ro.board.platform": platform,
"ro.product.model": model,
"abi": abi}
sh_commands.falcon_push_metrics(metrics, tags=tags,
endpoint="mace_ops_benchmark") endpoint="mace_ops_benchmark")
def parse_args(): def parse_args():
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
import argparse import argparse
import hashlib import hashlib
import os import os
import sh
import shutil import shutil
import subprocess import subprocess
import sys import sys
...@@ -111,18 +112,42 @@ def build_mace_run(production_mode, model_output_dir, hexagon_mode): ...@@ -111,18 +112,42 @@ def build_mace_run(production_mode, model_output_dir, hexagon_mode):
run_command(command) run_command(command)
def tuning_run(target_soc, def tuning_run(model_name,
target_runtime,
target_abi,
target_soc,
model_output_dir, model_output_dir,
running_round, running_round,
tuning, tuning,
production_mode, production_mode,
restart_round, restart_round,
option_args=''): option_args=''):
command = "bash tools/tuning_run.sh {} {} {} {} {} {} \"{}\"".format( # TODO(yejianwu) refactoring the hackish code
target_soc, model_output_dir, running_round, int(tuning), stdout_buff = []
int(production_mode), restart_round, option_args) process_output = sh_commands.make_output_processor(stdout_buff)
run_command(command) p = sh.bash("tools/tuning_run.sh", target_soc, model_output_dir,
running_round, int(tuning), int(production_mode),
restart_round, option_args, _out=process_output,
_bg=True, _err_to_out=True)
p.wait()
metrics = {}
for line in stdout_buff:
line = line.strip()
parts = line.split()
if len(parts) == 6 and parts[0].startswith("time"):
metrics["%s.create_net_ms" % model_name] = str(float(parts[1]))
metrics["%s.mace_engine_ctor_ms" % model_name] = str(float(parts[2]))
metrics["%s.init_ms" % model_name] = str(float(parts[3]))
metrics["%s.warmup_ms" % model_name] = str(float(parts[4]))
if float(parts[5]) > 0:
metrics["%s.avg_latency_ms" % model_name] = str(float(parts[5]))
tags = {"ro.board.platform": target_soc,
"abi": target_abi,
# "runtime": target_runtime, # TODO(yejianwu) Add the actual runtime
"round": running_round, # TODO(yejianwu) change this to source/binary
"tuning": tuning}
sh_commands.falcon_push_metrics(metrics, endpoint="mace_model_benchmark",
tags=tags)
def benchmark_model(target_soc, model_output_dir, option_args=''): def benchmark_model(target_soc, model_output_dir, option_args=''):
command = "bash tools/benchmark.sh {} {} \"{}\"".format( command = "bash tools/benchmark.sh {} {} \"{}\"".format(
...@@ -130,9 +155,10 @@ def benchmark_model(target_soc, model_output_dir, option_args=''): ...@@ -130,9 +155,10 @@ def benchmark_model(target_soc, model_output_dir, option_args=''):
run_command(command) run_command(command)
def run_model(target_soc, model_output_dir, running_round, restart_round, def run_model(model_name, target_runtime, target_abi, target_soc,
option_args): model_output_dir, running_round, restart_round, option_args):
tuning_run(target_soc, model_output_dir, running_round, False, False, tuning_run(model_name, target_runtime, target_abi, target_soc,
model_output_dir, running_round, False, False,
restart_round, option_args) restart_round, option_args)
...@@ -146,8 +172,9 @@ def generate_production_code(target_soc, model_output_dirs, pull_or_not): ...@@ -146,8 +172,9 @@ def generate_production_code(target_soc, model_output_dirs, pull_or_not):
run_command(command) run_command(command)
def build_mace_run_prod(target_soc, model_output_dir, tuning, global_runtime): def build_mace_run_prod(model_name, target_runtime, target_abi, target_soc,
if "dsp" == global_runtime: model_output_dir, tuning):
if "dsp" == target_runtime:
hexagon_mode = True hexagon_mode = True
else: else:
hexagon_mode = False hexagon_mode = False
...@@ -155,6 +182,9 @@ def build_mace_run_prod(target_soc, model_output_dir, tuning, global_runtime): ...@@ -155,6 +182,9 @@ def build_mace_run_prod(target_soc, model_output_dir, tuning, global_runtime):
production_or_not = False production_or_not = False
build_mace_run(production_or_not, model_output_dir, hexagon_mode) build_mace_run(production_or_not, model_output_dir, hexagon_mode)
tuning_run( tuning_run(
model_name,
target_runtime,
target_abi,
target_soc, target_soc,
model_output_dir, model_output_dir,
running_round=0, running_round=0,
...@@ -346,12 +376,13 @@ def main(unused_args): ...@@ -346,12 +376,13 @@ def main(unused_args):
if FLAGS.mode == "build" or FLAGS.mode == "all": if FLAGS.mode == "build" or FLAGS.mode == "all":
generate_model_code() generate_model_code()
build_mace_run_prod(target_soc, model_output_dir, FLAGS.tuning, build_mace_run_prod(model_name, global_runtime, target_abi,
global_runtime) target_soc, model_output_dir, FLAGS.tuning)
if FLAGS.mode == "run" or FLAGS.mode == "validate" or FLAGS.mode == "all": if FLAGS.mode == "run" or FLAGS.mode == "validate" or FLAGS.mode == "all":
run_model(target_soc, model_output_dir, FLAGS.round, run_model(model_name, global_runtime, target_abi, target_soc,
FLAGS.restart_round, option_args) model_output_dir, FLAGS.round, FLAGS.restart_round,
option_args)
if FLAGS.mode == "benchmark": if FLAGS.mode == "benchmark":
benchmark_model(target_soc, model_output_dir, option_args) benchmark_model(target_soc, model_output_dir, option_args)
......
...@@ -78,7 +78,7 @@ def adb_run(serialno, host_bin_path, bin_name, ...@@ -78,7 +78,7 @@ def adb_run(serialno, host_bin_path, bin_name,
sh.adb("-s", serialno, "shell", "mkdir -p %s" % device_bin_path) sh.adb("-s", serialno, "shell", "mkdir -p %s" % device_bin_path)
sh.adb("-s", serialno, "shell", "mkdir -p %s" % device_cl_path) sh.adb("-s", serialno, "shell", "mkdir -p %s" % device_cl_path)
print("Push %s to %s" % (host_bin_full_path, device_bin_full_path)) print("Push %s to %s" % (host_bin_full_path, device_bin_full_path))
sh.adb("-s", serialno, "push", host_bin_full_path, device_bin_path) sh.adb("-s", serialno, "push", host_bin_full_path, device_bin_full_path)
print("Run %s" % device_bin_full_path) print("Run %s" % device_bin_full_path)
stdout_buff=[] stdout_buff=[]
process_output = make_output_processor(stdout_buff) process_output = make_output_processor(stdout_buff)
...@@ -142,21 +142,24 @@ def gen_mace_version(codegen_path="mace/codegen"): ...@@ -142,21 +142,24 @@ def gen_mace_version(codegen_path="mace/codegen"):
################################ ################################
# falcon # falcon
################################ ################################
def falcon_tags(platform, model, abi): def falcon_tags(tags_dict):
return "ro.board.platform=%s,ro.product.model=%s,abi=%s" % (platform, model, abi) tags = ""
for k, v in tags_dict.iteritems():
def falcon_push_metrics(metrics, device_properties, abi, endpoint="mace_dev"): if tags == "":
tags = "%s=%s" % (k, v)
else:
tags = tags + ",%s=%s" % (k, v)
return tags
def falcon_push_metrics(metrics, endpoint="mace_dev", tags={}):
cli = falcon_cli.FalconCli.connect(server="transfer.falcon.miliao.srv", cli = falcon_cli.FalconCli.connect(server="transfer.falcon.miliao.srv",
port=8433, port=8433,
debug=False) debug=False)
platform = device_properties["ro.board.platform"].replace(" ", "-")
model = device_properties["ro.product.model"].replace(" ", "-")
tags = falcon_tags(platform, model, abi)
ts = int(time.time()) ts = int(time.time())
falcon_metrics = [{ falcon_metrics = [{
"endpoint": endpoint, "endpoint": endpoint,
"metric": key, "metric": key,
"tags": tags, "tags": falcon_tags(tags),
"timestamp": ts, "timestamp": ts,
"value": value, "value": value,
"step": 86400, "step": 86400,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册