Skip to content

Commit

Permalink
Merge branch 'fix_apu' into 'master'
Browse files Browse the repository at this point in the history
fix: fix so file selection error and op type selection errors on apu runtime

See merge request applied-machine-learning/sysml/mace!1341
  • Loading branch information
张志敏 committed Jan 10, 2021
2 parents d79dc74 + 0d27d59 commit d5ae855
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 25 deletions.
8 changes: 4 additions & 4 deletions mace/core/runtime/apu/apu_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ bool ApuWrapper::DoInit(const NetDef &net_def, unsigned const char *model_data,

// parse model argument
int const_data_num = 0;
int apu_data_type = -1;
int apu_dt = -1;
for (auto arg : net_def.arg()) {
if (arg.name().compare("const_data_num") == 0) {
const_data_num = arg.i();
} else if (arg.name().compare("apu_data_type") == 0) {
apu_data_type = arg.i();
apu_dt = arg.i();
}
}
// input tensors
Expand All @@ -104,7 +104,7 @@ bool ApuWrapper::DoInit(const NetDef &net_def, unsigned const char *model_data,
tensor.tensor_id = input_info.node_id();
tensor.tensor_type = APU_TENSOR_MODEL_INPUT;
tensor.data_type = static_cast<apu_data_type>(
MapToApuDataType(static_cast<DataType>(apu_data_type)));
MapToApuDataType(static_cast<DataType>(apu_dt)));
tensor.scale = input_info.has_scale() ? input_info.scale() : -1.0f;
tensor.zero_point = input_info.has_zero_point() ?
input_info.zero_point() : 0;
Expand Down Expand Up @@ -137,7 +137,7 @@ bool ApuWrapper::DoInit(const NetDef &net_def, unsigned const char *model_data,
tensor.tensor_id = output_info.node_id();
tensor.tensor_type = APU_TENSOR_MODEL_OUTPUT;
tensor.data_type = static_cast<apu_data_type>(
MapToApuDataType(static_cast<DataType>(apu_data_type)));
MapToApuDataType(static_cast<DataType>(apu_dt)));
tensor.dim_size = output_info.dims_size();
ApuTensorInfo info;
info.name = output_info.name();
Expand Down
4 changes: 2 additions & 2 deletions tools/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import sh_commands

sys.path.insert(0, "tools/python") # noqa
from utils.device import AndroidDevice
from copy_apu_so import get_apu_so_paths_by_props


class DeviceWrapper:
Expand Down Expand Up @@ -146,7 +146,7 @@ def get_apu_so_paths(self):
target_props = sh_commands.adb_getprop_by_serialno(self.address)
target_soc = target_props["ro.board.platform"]
android_ver = (int)(target_props["ro.build.version.release"])
return AndroidDevice.get_apu_so_paths_by_props(android_ver, target_soc)
return get_apu_so_paths_by_props(android_ver, target_soc)

def pull(self, src_path, file_name, dst_path='.'):
if not os.path.exists(dst_path):
Expand Down
1 change: 1 addition & 0 deletions tools/generate_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def generate_input_data(input_file, input_node, input_shape, input_ranges,
input_ranges = [r for r in input_ranges.split(':')]
else:
input_ranges = ["-1,1"] * len(input_names)
print("The scope of generated data: ", input_ranges)
if input_data_type:
input_data_types = [data_type
for data_type in input_data_type.split(',')]
Expand Down
8 changes: 6 additions & 2 deletions tools/python/run_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import shutil
import numpy as np

from copy_apu_so import get_apu_so_paths
from py_proto import mace_pb2
from utils import util
from utils import device
Expand Down Expand Up @@ -100,6 +101,8 @@ def run_model_for_device(flags, args, dev, model_name, model_conf):
runtime = DeviceType.GPU
else:
runtime = config_parser.parse_device_type(runtime)
mace_check(runtime != DeviceType.APU or target_abi == "arm64-v8a",
"APU runtime does only support arm64-v8a")

# install models to devices
workdir = flags.output + "/" + model_name
Expand Down Expand Up @@ -197,7 +200,7 @@ def run_model_for_device(flags, args, dev, model_name, model_conf):
if model_conf[ModelKeys.runtime] == DeviceType.HEXAGON:
libs += ["third_party/nnlib/%s/libhexagon_controller.so" % target_abi]
elif model_conf[ModelKeys.runtime] == DeviceType.APU:
apu_libs = dev.get_apu_so_paths()
apu_libs = get_apu_so_paths(dev)
libs += apu_libs

target = Target(build_dir + "/install/bin/mace_run", libs,
Expand Down Expand Up @@ -259,6 +262,7 @@ def run_model_for_device(flags, args, dev, model_name, model_conf):
def generate_input_data(input_file, input_node, input_shape, input_ranges,
input_data_type):
np.random.seed()
print("The scope of generated data: ", input_ranges)
for i in range(len(input_node)):
data = np.random.random(input_shape[i]) * (
input_ranges[i][1] - input_ranges[i][0]) + input_ranges[i][0]
Expand Down Expand Up @@ -289,7 +293,7 @@ def parse_args():
parser.add_argument(
"--target_abi",
type=str,
default="armeabi-v7a",
default="arm64-v8a",
help="Target ABI: host, armeabi-v7a, arm64-v8a,"
" arm-linux-gnueabihf, aarch64-linux-gnu"
)
Expand Down
12 changes: 9 additions & 3 deletions tools/python/transform/apu_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,13 @@ def convert_ops(self):
op.type = self._apu_ops.map_nn_op(op.type)
self.change_activation_to_prelu()

def get_input_type(self, op, type_map):
mace_check(len(op.input) > 0, "op %s has no input" % op.name)
for input in op.input:
if input in type_map:
return type_map[input]
mace_check(False, "op %s have no inputs in type_map" % op.name)

def add_op_output_type(self):
type_map = {}
for input_info in self._model.input_info:
Expand All @@ -310,9 +317,8 @@ def add_op_output_type(self):
print([op.name, len(op.output), len(op.output_type)])
type_map[op.output[0]] = op.output_type[0]
continue
mace_check(op.input[0] in type_map,
op.input[0] + ' not in type_map')
op.output_type.extend([type_map[op.input[0]]])
input_type = self.get_input_type(op, type_map)
op.output_type.extend([input_type])
type_map[op.output[0]] = op.output_type[0]

for op in self._model.op:
Expand Down
26 changes: 13 additions & 13 deletions tools/python/transform/keras_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,19 @@ def conv_output_length(input_length,
padding,
stride,
dilation=1):
if input_length is None:
return None

mace_check(padding in {'same', 'valid', 'full', 'causal'},
"Not supported padding type: %s" % padding)
dilated_filter_size = filter_size + (filter_size - 1) * (dilation - 1)
if padding in ['same', 'causal']:
output_length = input_length
elif padding == 'valid':
output_length = input_length - dilated_filter_size + 1
elif padding == 'full':
output_length = input_length + dilated_filter_size - 1
return (output_length + stride - 1) // stride
if input_length is None:
return None

mace_check(padding in {'same', 'valid', 'full', 'causal'},
"Not supported padding type: %s" % padding)
dilated_filter_size = filter_size + (filter_size - 1) * (dilation - 1)
if padding in ['same', 'causal']:
output_length = input_length
elif padding == 'valid':
output_length = input_length - dilated_filter_size + 1
elif padding == 'full':
output_length = input_length + dilated_filter_size - 1
return (output_length + stride - 1) # stride


activation_types_dict = {
Expand Down
3 changes: 2 additions & 1 deletion tools/python/transform/tensorflow_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,6 @@ def is_commutative(eltwise_type):
value_arg = op.arg.add()
value_arg.name = MaceKeyword.mace_scalar_input_str
value_arg.f = scalar
self._skip_tensor.add(tf_op.inputs[1].name)
value_index_arg = op.arg.add()
value_index_arg.name = \
MaceKeyword.mace_scalar_input_index_str
Expand Down Expand Up @@ -938,6 +937,7 @@ def convert_expand_dims(self, tf_op):
axis_arg = op.arg.add()
axis_arg.name = MaceKeyword.mace_axis_str
axis_arg.i = axis_value
self._skip_tensor.add(tf_op.inputs[1].name)
del op.input[1]

def convert_squeeze(self, tf_op):
Expand Down Expand Up @@ -1180,6 +1180,7 @@ def convert_cumsum(self, tf_op):
axis_arg = op.arg.add()
axis_arg.name = MaceKeyword.mace_axis_str
axis_arg.i = axis
self._skip_tensor.add(tf_op.inputs[1].name)
del op.input[1]

exclusive = tf_op.get_attr('exclusive')
Expand Down

0 comments on commit d5ae855

Please sign in to comment.