diff --git a/mace/examples/BUILD b/mace/examples/BUILD index 96843a2e5bd0886299ad4b0f84f9811baba6ead5..7b6a268d56a78bce9879d9707d71f9f8eb4f29c6 100644 --- a/mace/examples/BUILD +++ b/mace/examples/BUILD @@ -39,15 +39,3 @@ cc_binary( "//mace/utils:command_line_flags", ], ) - -cc_binary( - name = "example", - srcs = ["example.cc"], - copts = ["-std=c++11", "-D_GLIBCXX_USE_C99_MATH_TR1"], - linkopts = ["-fopenmp"], - linkstatic = 1, - deps = [ - "//mace/codegen:generated_models_lib", - "//mace/utils:command_line_flags", - ], -) diff --git a/mace/examples/example.cc b/mace/examples/example.cc deleted file mode 100644 index 5dbd2c4440a6e6f5b9133ad57165a77076d8df2a..0000000000000000000000000000000000000000 --- a/mace/examples/example.cc +++ /dev/null @@ -1,145 +0,0 @@ -// -// Copyright (c) 2017 XiaoMi All rights reserved. -// - -/** - * Usage: - * mace_run --model=mobi_mace.pb \ - * --input=input_node \ - * --output=MobilenetV1/Logits/conv2d/convolution \ - * --input_shape=1,3,224,224 \ - * --input_file=input_data \ - * --output_file=mace.out \ - * --device=NEON - */ -#include -#include -#include -#include -#include -#include "mace/utils/command_line_flags.h" -#include "mace/core/mace.h" -#include "mace/utils/logging.h" - -using namespace std; -using namespace mace; - -namespace mace { -extern NetDef MACE_MODEL_FUNCTION(); -} -void ParseShape(const string &str, vector *shape) { - string tmp = str; - while (!tmp.empty()) { - int dim = atoi(tmp.data()); - shape->push_back(dim); - size_t next_offset = tmp.find(","); - if (next_offset == string::npos) { - break; - } else { - tmp = tmp.substr(next_offset + 1); - } - } -} - -DeviceType ParseDeviceType(const string &device_str) { - if(device_str.compare("CPU") == 0) { - return DeviceType::CPU; - } else if (device_str.compare("NEON") == 0) { - return DeviceType::NEON; - } else if (device_str.compare("OPENCL") == 0) { - return DeviceType::OPENCL; - } else { - return DeviceType::CPU; - } -} - -int main(int argc, char **argv) { - string model_file; - string input_node; - string output_node; - string input_shape; - string input_file; - string output_file; - string device; - int round = 1; - - std::vector flag_list = { - Flag("model", &model_file, "model file name"), - Flag("input", &input_node, "input node"), - Flag("output", &output_node, "output node"), - Flag("input_shape", &input_shape, "input shape, separated by comma"), - Flag("input_file", &input_file, "input file name"), - Flag("output_file", &output_file, "output file name"), - Flag("device", &device, "CPU/NEON"), - Flag("round", &round, "round"), - }; - - string usage = Flags::Usage(argv[0], flag_list); - const bool parse_result = Flags::Parse(&argc, argv, flag_list); - - if (!parse_result) { - LOG(ERROR) << usage; - return -1; - } - - VLOG(0) << "model: " << model_file << std::endl - << "input: " << input_node << std::endl - << "output: " << output_node << std::endl - << "input_shape: " << input_shape << std::endl - << "input_file: " << input_file << std::endl - << "output_file: " << output_file << std::endl - << "device: " << device << std::endl - << "round: " << round << std::endl; - - vector shape; - ParseShape(input_shape, &shape); - - // load model - NetDef net_def = mace::MACE_MODEL_FUNCTION(); - - DeviceType device_type = ParseDeviceType(device); - VLOG(0) << device_type; - int64_t input_size = std::accumulate(shape.begin(), shape.end(), 1, std::multiplies()); - std::unique_ptr input_data(new float[input_size]); - - // load input - ifstream in_file(input_file, ios::in | ios::binary); - in_file.read(reinterpret_cast(input_data.get()), - input_size * sizeof(float)); - in_file.close(); - - // Init model - VLOG(0) << "Run init"; - mace::MaceEngine engine(&net_def, device_type); - - VLOG(0) << "Run model"; - std::vector output_shape; - timeval tv1, tv2; - gettimeofday(&tv1, NULL); - for (int i = 0; i < round; ++i) { - engine.Run(input_data.get(), shape, output_shape); - } - gettimeofday(&tv2, NULL); - std::cout << "avg duration: " - << ((tv2.tv_sec - tv1.tv_sec) * 1000 + - (tv2.tv_usec - tv1.tv_usec) / 1000) / - round - << endl; - - const float *output = engine.Run(input_data.get(), shape, output_shape); - if (output != nullptr) { - ofstream out_file(output_file, ios::binary); - int64_t output_size = std::accumulate(output_shape.begin(), output_shape.end(), 1, std::multiplies()); - out_file.write((const char *) (output), - output_size * sizeof(float)); - out_file.flush(); - out_file.close(); - stringstream ss; - ss << "Output shape: ["; - for (auto i : output_shape) { - ss << i << ", "; - } - ss << "]"; - VLOG(0) << ss.str(); - } -} \ No newline at end of file diff --git a/mace/examples/mace_run.cc b/mace/examples/mace_run.cc index d62fc1229a670f6920c3025ffc4b1352f04b1ea4..d6fb321e70c4d36c6d8cf57fc30d44a29eb7c9ef 100644 --- a/mace/examples/mace_run.cc +++ b/mace/examples/mace_run.cc @@ -12,11 +12,14 @@ * --output_file=mace.out \ * --device=NEON */ +#include #include -#include "mace/core/net.h" -#include "mace/core/workspace.h" +#include +#include +#include #include "mace/utils/command_line_flags.h" -#include "mace/utils/env_time.h" +#include "mace/core/mace.h" +#include "mace/utils/logging.h" using namespace std; using namespace mace; @@ -24,7 +27,7 @@ using namespace mace; namespace mace { extern NetDef MACE_MODEL_FUNCTION(); } -void ParseShape(const string &str, vector *shape) { +void ParseShape(const string &str, vector *shape) { string tmp = str; while (!tmp.empty()) { int dim = atoi(tmp.data()); @@ -88,86 +91,61 @@ int main(int argc, char **argv) { << "device: " << device << std::endl << "round: " << round << std::endl; - vector shape; + vector shape; ParseShape(input_shape, &shape); - int64_t t0 = utils::NowMicros(); + // load model NetDef net_def = mace::MACE_MODEL_FUNCTION(); - int64_t t1 = utils::NowMicros(); - LOG(INFO) << "CreateNetDef duration: " << t1 - t0 << "us"; - int64_t init_micros = t1 - t0; - t0 = utils::NowMicros(); DeviceType device_type = ParseDeviceType(device); - VLOG(1) << "Device Type" << device_type; - Workspace ws; - ws.LoadModelTensor(net_def, device_type); - Tensor *input_tensor = - ws.CreateTensor(input_node + ":0", GetDeviceAllocator(device_type), DT_FLOAT); - input_tensor->Resize(shape); - t1 = utils::NowMicros(); - init_micros += t1 - t0; - LOG(INFO) << "CreateWorkspaceTensor duration: " << t1 - t0 << "us"; + VLOG(0) << device_type; + int64_t input_size = std::accumulate(shape.begin(), shape.end(), 1, std::multiplies()); + std::unique_ptr input_data(new float[input_size]); + + // load input + ifstream in_file(input_file, ios::in | ios::binary); + in_file.read(reinterpret_cast(input_data.get()), + input_size * sizeof(float)); + in_file.close(); // Init model VLOG(0) << "Run init"; - t0 = utils::NowMicros(); - auto net = CreateNet(net_def, &ws, device_type, NetMode::INIT); - net->Run(); - t1 = utils::NowMicros(); - init_micros += t1 - t0; - LOG(INFO) << "Net init duration: " << t1 - t0 << "us"; - - // run model - t0 = utils::NowMicros(); - net = CreateNet(net_def, &ws, device_type); - t1 = utils::NowMicros(); - init_micros += t1 - t0; - LOG(INFO) << "Total init duration: " << init_micros << "us"; - - { - Tensor::MappingGuard input_guard(input_tensor); - float *input_data = input_tensor->mutable_data(); - - // load input - ifstream in_file(input_file, ios::in | ios::binary); - in_file.read(reinterpret_cast(input_data), - input_tensor->size() * sizeof(float)); - in_file.close(); - } + mace::MaceEngine engine(&net_def, device_type); + std::vector output_shape; + VLOG(0) << "warm up"; // warm up - VLOG(0) << "Warm up"; - t0 = utils::NowMicros(); - net->Run(); - t1 = utils::NowMicros(); - LOG(INFO) << "1st run duration: " << t1 - t0 << "us"; - - VLOG(0) << "Run"; - t0 = utils::NowMicros(); - for (int i = 0; i < round; ++i) { - net->Run(); + for (int i = 0; i < 1; ++i) { + engine.Run(input_data.get(), shape, output_shape); } - t1 = utils::NowMicros(); - LOG(INFO) << "Average duration: " << (t1 - t0) / round << "us"; - - // save output - const Tensor *output = ws.GetTensor(output_node + ":0"); - std::remove(output_file.c_str()); + VLOG(0) << "Run model"; + timeval tv1, tv2; + gettimeofday(&tv1, NULL); + for (int i = 0; i < round; ++i) { + engine.Run(input_data.get(), shape, output_shape); + } + gettimeofday(&tv2, NULL); + std::cout << "avg duration: " + << ((tv2.tv_sec - tv1.tv_sec) * 1000 + + (tv2.tv_usec - tv1.tv_usec) / 1000) / + round + << endl; + + const float *output = engine.Run(input_data.get(), shape, output_shape); if (output != nullptr) { - Tensor::MappingGuard output_guard(output); ofstream out_file(output_file, ios::binary); - out_file.write((const char *)(output->data()), - output->size() * sizeof(float)); + int64_t output_size = std::accumulate(output_shape.begin(), output_shape.end(), 1, std::multiplies()); + out_file.write((const char *) (output), + output_size * sizeof(float)); out_file.flush(); out_file.close(); stringstream ss; ss << "Output shape: ["; - for (int i = 0; i < output->dim_size(); ++i) { - ss << output->dim(i) << ", "; + for (auto i : output_shape) { + ss << i << ", "; } ss << "]"; VLOG(0) << ss.str(); } -} +} \ No newline at end of file diff --git a/tools/validate_example.sh b/tools/validate_example.sh deleted file mode 100644 index 4957b45651752001197d6e2c2e6d058359069075..0000000000000000000000000000000000000000 --- a/tools/validate_example.sh +++ /dev/null @@ -1,128 +0,0 @@ -#!/bin/bash -# Must run at root dir of mace project. -set +x -Usage() { - echo 'Usage: bash tools/validate_gcn.sh tf_model_path image_size [tuning]' -} - -if [ $# -lt 2 ];then - Usage - exit -1 -fi - -TF_MODEL_FILE_PATH=$1 -MODEL_DIR=$(dirname ${TF_MODEL_FILE_PATH}) -MACE_SOURCE_DIR=`/bin/pwd` -MACE_MODEL_NAME='mace_model.pb' -INPUT_FILE_NAME='model_input' -OUTPUT_FILE_NAME='gcn.out' -OUTPUT_LIST_FILE='gcn.list' -PHONE_DATA_DIR="/data/local/tmp/${MACE_MODEL_NAME}" -KERNEL_DIR="${PHONE_DATA_DIR}/cl/" -IMAGE_SIZE=$2 -MODEL_TAG=GCN${IMAGE_SIZE} -CODEGEN_DIR=${MACE_SOURCE_DIR}/mace/codegen -MODEL_CODEGEN_DIR=${CODEGEN_DIR}/models/gcn-$IMAGE_SIZE -CL_CODEGEN_DIR=${CODEGEN_DIR}/opencl -CL_BIN_DIR=${CODEGEN_DIR}/opencl_bin -TUNING_CODEGEN_DIR=${CODEGEN_DIR}/tuning -TUNING_OR_NOT=${3:-0} - -build_and_run() -{ - EMBED_OPENCL_BINARY=$1 - if [ "$EMBED_OPENCL_BINARY" = true ]; then - EMBED_OPENCL_BINARY_BUILD_FLAGS="--define embed_binary_program=true" - fi - - bazel build -c opt --strip always mace/examples:example \ - --crosstool_top=//external:android/crosstool \ - --host_crosstool_top=@bazel_tools//tools/cpp:toolchain \ - --cpu=arm64-v8a \ - $EMBED_OPENCL_BINARY_BUILD_FLAGS \ - --copt=-DMACE_MODEL_FUNCTION=Create${MODEL_TAG} - - adb shell "mkdir -p ${PHONE_DATA_DIR}" - if [ "$EMBED_OPENCL_BINARY" = false ]; then - adb shell "rm -rf ${KERNEL_DIR}" - adb shell "mkdir -p ${KERNEL_DIR}" - adb push mace/kernels/opencl/cl/. ${KERNEL_DIR} - fi - adb push ${MODEL_DIR}/${INPUT_FILE_NAME} ${PHONE_DATA_DIR} - adb push bazel-bin/mace/examples/example ${PHONE_DATA_DIR} - - num_threads=${1:-4} - if [[ "${TUNING_OR_NOT}" != "0" && "$EMBED_OPENCL_BINARY" != true ]];then - tuning_flag=1 - else - tuning_flag=0 - fi - - adb