Skip to content

Commit 00bc304

Browse files
committed
feat: Support ONNX models to Hexagon and add some Hexagon ops
1 parent f253b85 commit 00bc304

File tree

15 files changed

+458
-170
lines changed

15 files changed

+458
-170
lines changed

docs/installation/env_requirement.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Optional dependencies
7474
- pip install filelock==3.0.0
7575
- Required by run on Android
7676
* - ONNX
77-
- pip install onnx==1.5.0
77+
- pip install onnx==1.8.0
7878
- Required by ONNX model
7979

8080
For python dependencies,

docs/micro-controllers/basic_usage.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Basic usage for Micro Controllers
22
==================================
33

4-
MACE Micro is a lightweight neural network inference engine for MCUs and low-power DSPs.
5-
At now we support Cortex-M MCUs and Qualcomm Hexagon DSPs. You can get our projects from GitHub.
4+
MACE Micro is a lightweight neural network inference engine for MCUs.
5+
At now we support Cortex-M MCUs. You can get our projects from GitHub.
66

77
Get MACE Micro Projects
88
-----------------------

docs/micro-controllers/deploy.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,3 @@ You can use the Mace Micro install package("build/micro/gcc-arm-none-eabi/instal
4141
mbed sterm
4242
4343
Press the reset(black) button to run the example again.
44-
45-
For Hexagon DSP
46-
---------------
47-
48-
In the micro/cmake/toolchain folder, there are two hexagon CMake toolchain files for reference, For more details, please goto <https://developer.qualcomm.com/software/hexagon-dsp-sdk/dsp-processor/>

mace/core/runtime/hexagon/hexagon_dsp_wrapper.cc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -558,23 +558,18 @@ bool HexagonDSPWrapper::ExecuteGraphNew(
558558
// handle hexagon output
559559
for (size_t i = 0; i < num_outputs; ++i) {
560560
size_t index = i * kNumMetaData;
561-
std::vector<uint32_t> output_shape{
561+
std::vector<index_t> output_shape{
562562
outputs[index].batches, outputs[index].height, outputs[index].width,
563563
outputs[index].depth};
564564
MACE_CHECK(output_shape.size() == output_info_[i].shape.size(),
565565
output_shape.size(), " vs ", output_info_[i].shape.size(),
566566
" wrong output shape inferred");
567-
for (size_t j = 0; j < output_shape.size(); ++j) {
568-
MACE_CHECK(static_cast<index_t>(output_shape[j])
569-
== output_info_[i].shape[j],
570-
output_shape[j], " vs ", output_info_[i].shape[j],
571-
" wrong output shape[", j, "] inferred");
572-
}
573567
auto output_tensor = output_tensors->at(output_info_[i].name);
574568
MACE_CHECK(static_cast<index_t>(outputs[index].data_valid_len)
575569
== output_tensor->raw_size(),
576570
outputs[index].data_valid_len, " vs ", output_tensor->raw_size(),
577571
" wrong output bytes inferred.");
572+
output_tensor->Reshape(output_shape);
578573
}
579574

580575
if (log_execute_time_) {

mace/libmace/mace.cc

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "mace/core/registry/op_delegator_registry.h"
2626
#include "mace/ops/common/transpose.h"
2727
#include "mace/ops/registry/registry.h"
28+
#include "mace/utils/conf_util.h"
2829
#include "mace/utils/math.h"
2930
#include "mace/utils/memory.h"
3031
#include "mace/utils/stl_util.h"
@@ -793,7 +794,7 @@ MaceEngine::Impl::~Impl() {
793794
if (VLOG_IS_ON(2)) {
794795
hexagon_controller_->PrintLog();
795796
}
796-
if (VLOG_IS_ON(1)) {
797+
if (EnvConfEnabled("MACE_HEXAGON_PROFILING")) {
797798
hexagon_controller_->GetPerfInfo();
798799
}
799800
MACE_CHECK(hexagon_controller_->TeardownGraph(), "hexagon teardown error");
@@ -928,24 +929,20 @@ MaceStatus MaceEngine::Impl::TransposeOutput(
928929
output->second.data_format() != DataFormat::NONE &&
929930
output->second.shape().size() == 4 &&
930931
output->second.data_format() != output_tensor->data_format()) {
931-
VLOG(1) << "Transform output " << output->first << " from "
932-
<< static_cast<int>(output_tensor->data_format()) << " to "
933-
<< static_cast<int>(output->second.data_format());
934932
std::vector<int> dst_dims;
935933
if (output_tensor->data_format() == DataFormat::NCHW &&
936934
output->second.data_format() == DataFormat::NHWC) {
937935
dst_dims = {0, 2, 3, 1};
936+
VLOG(1) << "Transform output " << output->first << " from NCHW to NHWC";
938937
} else if (output_tensor->data_format() == DataFormat::NHWC &&
939938
output->second.data_format() == DataFormat::NCHW) {
940939
dst_dims = {0, 3, 1, 2};
940+
VLOG(1) << "Transform output " << output->first << " from NHWC to NCHW";
941941
} else {
942942
LOG(FATAL) << "Not supported output data format: "
943943
<< static_cast<int>(output->second.data_format()) << " vs "
944944
<< static_cast<int>(output_tensor->data_format());
945945
}
946-
VLOG(1) << "Transform output " << output->first << " from "
947-
<< static_cast<int>(output_tensor->data_format()) << " to "
948-
<< static_cast<int>(output->second.data_format());
949946
std::vector<index_t> shape =
950947
TransposeShape<index_t, index_t>(output_tensor->shape(),
951948
dst_dims);

mace/tools/mace_run.cc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ bool RunModel(const std::string &model_name,
171171
const std::vector<DataFormat> &output_data_formats,
172172
float cpu_capability) {
173173
DeviceType device_type = ParseDeviceType(FLAGS_device);
174+
bool mace_benchmark_op = false;
175+
if (FLAGS_benchmark &&
176+
(device_type == DeviceType::CPU || device_type == DeviceType::GPU)) {
177+
mace_benchmark_op = true;
178+
}
174179

175180
int64_t t0 = NowMicros();
176181
// config runtime
@@ -448,7 +453,7 @@ bool RunModel(const std::string &model_name,
448453
MaceStatus run_status;
449454
RunMetadata metadata;
450455
RunMetadata *metadata_ptr = nullptr;
451-
if (FLAGS_benchmark) {
456+
if (mace_benchmark_op) {
452457
metadata_ptr = &metadata;
453458
}
454459

@@ -488,7 +493,7 @@ bool RunModel(const std::string &model_name,
488493
} else {
489494
int64_t t1 = NowMicros();
490495
total_run_duration += (t1 - t0);
491-
if (FLAGS_benchmark) {
496+
if (mace_benchmark_op) {
492497
op_stat.StatMetadata(metadata);
493498
}
494499
break;
@@ -521,7 +526,7 @@ bool RunModel(const std::string &model_name,
521526
printf("========================================================\n");
522527
printf("time %15.3f %11.3f %11.3f %11.3f\n",
523528
cpu_capability, init_millis, warmup_millis, model_run_millis);
524-
if (FLAGS_benchmark) {
529+
if (mace_benchmark_op) {
525530
op_stat.PrintStat();
526531
}
527532
}
@@ -545,14 +550,17 @@ int Main(int argc, char **argv) {
545550

546551
if (FLAGS_benchmark) {
547552
setenv("MACE_OPENCL_PROFILING", "1", 1);
553+
setenv("MACE_HEXAGON_PROFILING", "1", 1);
548554
}
549555

550556
LOG(INFO) << "model name: " << FLAGS_model_name;
551557
LOG(INFO) << "mace version: " << MaceVersion();
552558
LOG(INFO) << "input node: " << FLAGS_input_node;
553559
LOG(INFO) << "input shape: " << FLAGS_input_shape;
560+
LOG(INFO) << "input data_format: " << FLAGS_input_data_format;
554561
LOG(INFO) << "output node: " << FLAGS_output_node;
555562
LOG(INFO) << "output shape: " << FLAGS_output_shape;
563+
LOG(INFO) << "output data_format: " << FLAGS_output_data_format;
556564
LOG(INFO) << "input_file: " << FLAGS_input_file;
557565
LOG(INFO) << "output_file: " << FLAGS_output_file;
558566
LOG(INFO) << "input dir: " << FLAGS_input_dir;

tools/device.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ def run_model(self, flags, configs, target_abi,
571571
output_shapes=output_config[YAMLKeyword.output_shapes],
572572
input_data_formats=subgraphs[0][
573573
YAMLKeyword.input_data_formats],
574-
output_data_formats=subgraphs[0][
574+
output_data_formats=output_config[
575575
YAMLKeyword.output_data_formats],
576576
mace_model_dir=mace_model_dir,
577577
model_tag=model_name,
@@ -708,7 +708,9 @@ def run_specify_abi(self, flags, configs, target_abi):
708708
model_path = "%s/%s.pb" % (mace_model_dir, model_name)
709709
output_config = {YAMLKeyword.model_file_path: model_path,
710710
YAMLKeyword.output_tensors: output_nodes,
711-
YAMLKeyword.output_shapes: output_shapes}
711+
YAMLKeyword.output_shapes: output_shapes,
712+
YAMLKeyword.output_data_formats:
713+
subgraphs[0][YAMLKeyword.output_data_formats]}
712714
output_configs.append(output_config)
713715

714716
runtime_list = []
@@ -819,7 +821,7 @@ def run_specify_abi(self, flags, configs, target_abi):
819821
YAMLKeyword.output_shapes],
820822
input_data_formats=subgraphs[0][
821823
YAMLKeyword.input_data_formats],
822-
output_data_formats=subgraphs[0][
824+
output_data_formats=output_config[
823825
YAMLKeyword.output_data_formats],
824826
model_output_dir=model_output_dir,
825827
input_data_types=subgraphs[0][

tools/layers_validate.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import yaml
2020

2121
sys.path.insert(0, "tools/python") # noqa
22+
from common import DataFormat
2223
from py_proto import mace_pb2
2324
from transform.base_converter import ConverterUtil
2425
from transform.base_converter import MaceKeyword
@@ -129,11 +130,14 @@ def convert(model_file, output_dir, layers):
129130
output_tensors = []
130131
output_shapes = []
131132
op_name = op.name
133+
if str(op.name).startswith(MaceKeyword.mace_output_node_name):
134+
continue
132135
if is_quantize:
133136
op.name = MaceKeyword.mace_output_node_name + '_' + op.name
134137
if is_hexagon:
135-
mace_check(len(op.output) == 1,
136-
"Only supports number of outputs of Hexagon op be 1.")
138+
if len(op.output) != 1:
139+
print("Skip %s(%s)" % (op.name, op.type))
140+
continue
137141
for i in range(len(op.output)):
138142
output_tensors.append(str(op.output[i]))
139143
output_shapes.append(
@@ -187,7 +191,8 @@ def convert(model_file, output_dir, layers):
187191
output_dir)
188192
output_config = {"model_file_path": str(model_path),
189193
"output_tensors": output_tensors,
190-
"output_shapes": output_shapes}
194+
"output_shapes": output_shapes,
195+
"output_data_formats": [DataFormat.NHWC]}
191196
output_configs["subgraphs"].append(output_config)
192197

193198
output_configs_path = output_dir + "outputs.yml"

tools/python/layers_validate.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from transform.base_converter import MaceKeyword
2424
from transform.base_converter import MaceOp
2525
from transform.hexagon_converter import HexagonOp
26+
from utils.config_parser import DataFormat
2627
from utils.util import mace_check
2728

2829

@@ -125,11 +126,14 @@ def convert(model_file, output_dir, layers):
125126
output_tensors = []
126127
output_shapes = []
127128
op_name = op.name
129+
if str(op.name).startswith(MaceKeyword.mace_output_node_name):
130+
continue
128131
if is_quantize:
129132
op.name = MaceKeyword.mace_output_node_name + '_' + op.name
130133
if is_hexagon:
131-
mace_check(len(op.output) == 1,
132-
"Only supports number of outputs of Hexagon op be 1.")
134+
if len(op.output) != 1:
135+
print("Skip %s(%s)" % (op.name, op.type))
136+
continue
133137
for i in range(len(op.output)):
134138
output_tensors.append(str(op.output[i]))
135139
output_shapes.append(
@@ -183,7 +187,8 @@ def convert(model_file, output_dir, layers):
183187
output_dir)
184188
output_config = {"model_file_path": str(model_path),
185189
"output_tensors": output_tensors,
186-
"output_shapes": output_shapes}
190+
"output_shapes": output_shapes,
191+
"output_data_formats": [DataFormat.NHWC.name]}
187192
output_configs["subgraphs"].append(output_config)
188193

189194
output_configs_path = output_dir + "outputs.yml"

0 commit comments

Comments
 (0)