Exemple #1
0
def benchmark_model(flags):
    configs = format_model_config(flags)

    clear_build_dirs(configs[YAMLKeyword.library_name])

    target_socs = configs[YAMLKeyword.target_socs]
    device_list = DeviceManager.list_devices(flags.device_yml)
    if target_socs and TargetSOCTag.all not in target_socs:
        device_list = [dev for dev in device_list
                       if dev[YAMLKeyword.target_socs].lower() in target_socs]
    for target_abi in configs[YAMLKeyword.target_abis]:
        if flags.target_socs == TargetSOCTag.random:
            target_devices = sh_commands.choose_a_random_device(
                device_list, target_abi)
        else:
            target_devices = device_list
        # build benchmark_model binary
        for dev in target_devices:
            if target_abi in dev[YAMLKeyword.target_abis]:
                toolchain = infer_toolchain(target_abi)
                build_benchmark_model(configs,
                                      target_abi,
                                      toolchain,
                                      not flags.disable_openmp,
                                      flags.mace_lib_type)
                device = DeviceWrapper(dev)
                start_time = time.time()
                with device.lock():
                    device.bm_specific_target(flags, configs, target_abi)
                elapse_minutes = (time.time() - start_time) / 60
                print("Elapse time: %f minutes." % elapse_minutes)
            else:
                six.print_('There is no abi %s with soc %s' %
                           (target_abi, dev[YAMLKeyword.target_socs]),
                           file=sys.stderr)
Exemple #2
0
def benchmark_model(flags):
    configs = format_model_config(flags)

    clear_build_dirs(configs[YAMLKeyword.library_name])

    target_socs = configs[YAMLKeyword.target_socs]
    device_list = DeviceManager.list_devices(flags.device_yml)
    if target_socs and ALL_SOC_TAG not in target_socs:
        device_list = [
            dev for dev in device_list
            if dev[YAMLKeyword.target_socs].lower() in target_socs
        ]

    for target_abi in configs[YAMLKeyword.target_abis]:
        # build benchmark_model binary
        for dev in device_list:
            if target_abi in dev[YAMLKeyword.target_abis]:
                toolchain = infer_toolchain(target_abi)
                build_benchmark_model(configs, target_abi, toolchain,
                                      not flags.disable_openmp,
                                      flags.mace_lib_type)
                device = DeviceWrapper(dev)
                with device.lock():
                    device.bm_specific_target(flags, configs, target_abi)
            else:
                six.print_('There is no abi %s with soc %s' %
                           (target_abi, dev[YAMLKeyword.target_socs]),
                           file=sys.stderr)
Exemple #3
0
def main(unused_args):
    target = FLAGS.target
    host_bin_path, bin_name = sh_commands.bazel_target_to_bin(target)
    target_abis = FLAGS.target_abis.split(',')
    dana_util = DanaUtil()

    for target_abi in target_abis:
        toolchain = infer_toolchain(target_abi)
        sh_commands.bazel_build(target,
                                abi=target_abi,
                                toolchain=toolchain,
                                enable_neon=FLAGS.enable_neon,
                                enable_quantize=FLAGS.enable_quantize,
                                enable_bfloat16=FLAGS.enable_bfloat16,
                                enable_fp16=FLAGS.enable_fp16,
                                enable_rpcmem=FLAGS.enable_rpcmem,
                                enable_hta=FLAGS.enable_hta,
                                address_sanitizer=FLAGS.address_sanitizer,
                                debug_mode=FLAGS.debug_mode)
        if FLAGS.run_target:
            target_devices = DeviceManager.list_devices(FLAGS.device_yml)
            if FLAGS.target_socs != TargetSOCTag.all and \
                    FLAGS.target_socs != TargetSOCTag.random:
                target_socs = set(FLAGS.target_socs.split(','))
                target_devices = \
                    [dev for dev in target_devices
                     if dev[YAMLKeyword.target_socs] in target_socs]
            if FLAGS.target_socs == TargetSOCTag.random:
                target_devices = sh_commands.choose_a_random_device(
                    target_devices, target_abi)

            for dev in target_devices:
                if target_abi not in dev[YAMLKeyword.target_abis]:
                    print("Skip device %s which does not support ABI %s" %
                          (dev, target_abi))
                    continue
                device_wrapper = DeviceWrapper(dev)
                stdouts = device_wrapper.run(
                    target_abi,
                    host_bin_path,
                    bin_name,
                    args=FLAGS.args,
                    opencl_profiling=True,
                    vlog_level=FLAGS.vlog_level,
                    out_of_range_check=True,
                    address_sanitizer=FLAGS.address_sanitizer,
                    simpleperf=FLAGS.simpleperf)
                globals()[FLAGS.stdout_processor](stdouts, dev, target_abi)
                if dana_util.service_available():
                    report_run_statistics(stdouts=stdouts,
                                          device=dev['device_name'],
                                          soc=dev['target_socs'],
                                          abi=target_abi,
                                          dana_util=dana_util)
Exemple #4
0
def run_mace(flags):
    configs = format_model_config(flags)

    clear_build_dirs(configs[YAMLKeyword.library_name])

    target_socs = configs[YAMLKeyword.target_socs]
    device_list = DeviceManager.list_devices(flags.device_yml)
    if target_socs and TargetSOCTag.all not in target_socs:
        device_list = [dev for dev in device_list
                       if dev[YAMLKeyword.target_socs].lower() in target_socs]
    for target_abi in configs[YAMLKeyword.target_abis]:
        if flags.target_socs == TargetSOCTag.random:
            target_devices = sh_commands.choose_a_random_device(
                device_list, target_abi)
        else:
            target_devices = device_list
        # build target
        for dev in target_devices:
            if target_abi in dev[YAMLKeyword.target_abis]:
                # get toolchain
                toolchain = infer_toolchain(target_abi)
                device = DeviceWrapper(dev)
                if flags.example:
                    build_example(configs,
                                  target_abi,
                                  toolchain,
                                  not flags.disable_openmp,
                                  flags.mace_lib_type,
                                  flags.cl_binary_to_code,
                                  device)
                else:
                    build_mace_run(configs,
                                   target_abi,
                                   toolchain,
                                   not flags.disable_openmp,
                                   flags.address_sanitizer,
                                   flags.mace_lib_type)
                # run
                start_time = time.time()
                with device.lock():
                    device.run_specify_abi(flags, configs, target_abi)
                elapse_minutes = (time.time() - start_time) / 60
                print("Elapse time: %f minutes." % elapse_minutes)
            elif dev[YAMLKeyword.device_name] != SystemType.host:
                six.print_('The device with soc %s do not support abi %s' %
                           (dev[YAMLKeyword.target_socs], target_abi),
                           file=sys.stderr)

    # package the output files
    package_path = sh_commands.packaging_lib(BUILD_OUTPUT_DIR,
                                             configs[YAMLKeyword.library_name])
    print_package_summary(package_path)
Exemple #5
0
def main(unused_args):
    target = FLAGS.target
    host_bin_path, bin_name = sh_commands.bazel_target_to_bin(target)
    target_abis = FLAGS.target_abis.split(',')

    for target_abi in target_abis:
        toolchain = infer_toolchain(target_abi)
        sh_commands.bazel_build(target, abi=target_abi,
                                toolchain=toolchain,
                                enable_neon=FLAGS.enable_neon,
                                address_sanitizer=FLAGS.address_sanitizer)
        if FLAGS.run_target:
            target_devices = DeviceManager.list_devices(FLAGS.device_yml)
            if FLAGS.target_socs != "all" and FLAGS.target_socs != "random":
                target_socs = set(FLAGS.target_socs.split(','))
                target_devices = \
                    [dev for dev in target_devices
                     if dev[YAMLKeyword.target_socs] in target_socs]
            if FLAGS.target_socs == "random":
                unlocked_devices = \
                    [d for d in target_devices if
                     not sh_commands.is_device_locked(d)]
                if len(unlocked_devices) > 0:
                    target_devices = [random.choice(unlocked_devices)]
                else:
                    target_devices = [random.choice(target_devices)]

            for dev in target_devices:
                if target_abi not in dev[YAMLKeyword.target_abis]:
                    print("Skip device %s which does not support ABI %s" %
                          (dev, target_abi))
                    continue
                device_wrapper = DeviceWrapper(dev)
                stdouts = device_wrapper.run(
                    target_abi,
                    host_bin_path,
                    bin_name,
                    args=FLAGS.args,
                    opencl_profiling=True,
                    vlog_level=0,
                    out_of_range_check=True,
                    address_sanitizer=FLAGS.address_sanitizer,
                    simpleperf=FLAGS.simpleperf)
                globals()[FLAGS.stdout_processor](stdouts, dev,
                                                  target_abi)
Exemple #6
0
def run_mace(flags):
    configs = format_model_config(flags)

    clear_build_dirs(configs[YAMLKeyword.library_name])

    target_socs = configs[YAMLKeyword.target_socs]
    device_list = DeviceManager.list_devices(flags.device_yml)
    if target_socs and ALL_SOC_TAG not in target_socs:
        device_list = [
            dev for dev in device_list
            if dev[YAMLKeyword.target_socs].lower() in target_socs
        ]
    for target_abi in configs[YAMLKeyword.target_abis]:
        # build target
        for dev in device_list:
            if target_abi in dev[YAMLKeyword.target_abis]:
                # get toolchain
                toolchain = infer_toolchain(target_abi)
                if flags.example:
                    build_example(configs, target_abi, toolchain,
                                  not flags.disable_openmp,
                                  flags.mace_lib_type)
                else:
                    build_mace_run(configs, target_abi, toolchain,
                                   not flags.disable_openmp,
                                   flags.address_sanitizer,
                                   flags.mace_lib_type)
                # run
                device = DeviceWrapper(dev)
                with device.lock():
                    device.run_specify_abi(flags, configs, target_abi)
            elif dev[YAMLKeyword.device_name] != SystemType.host:
                six.print_('The device with soc %s do not support abi %s' %
                           (dev[YAMLKeyword.target_socs], target_abi),
                           file=sys.stderr)

    # package the output files
    package_path = sh_commands.packaging_lib(BUILD_OUTPUT_DIR,
                                             configs[YAMLKeyword.library_name])
    print_package_summary(package_path)
Exemple #7
0
def format_model_config(flags):
    with open(flags.config) as f:
        configs = yaml.load(f)

    library_name = configs.get(YAMLKeyword.library_name, "")
    mace_check(
        len(library_name) > 0, ModuleName.YAML_CONFIG,
        "library name should not be empty")

    if flags.target_abis:
        target_abis = flags.target_abis.split(',')
    else:
        target_abis = configs.get(YAMLKeyword.target_abis, [])
    mace_check((isinstance(target_abis, list) and len(target_abis) > 0),
               ModuleName.YAML_CONFIG, "target_abis list is needed")
    configs[YAMLKeyword.target_abis] = target_abis
    for abi in target_abis:
        mace_check(abi in ABITypeStrs, ModuleName.YAML_CONFIG,
                   "target_abis must be in " + str(ABITypeStrs))

    target_socs = configs.get(YAMLKeyword.target_socs, "")
    if flags.target_socs and flags.target_socs != TargetSOCTag.random \
            and flags.target_socs != TargetSOCTag.all:
        configs[YAMLKeyword.target_socs] = \
            [soc.lower() for soc in flags.target_socs.split(',')]
    elif not target_socs:
        configs[YAMLKeyword.target_socs] = []
    elif not isinstance(target_socs, list):
        configs[YAMLKeyword.target_socs] = [target_socs]

    configs[YAMLKeyword.target_socs] = \
        [soc.lower() for soc in configs[YAMLKeyword.target_socs]]

    if ABIType.armeabi_v7a in target_abis \
            or ABIType.arm64_v8a in target_abis:
        available_socs = sh_commands.adb_get_all_socs()
        target_socs = configs[YAMLKeyword.target_socs]
        if TargetSOCTag.all in target_socs:
            mace_check(
                available_socs, ModuleName.YAML_CONFIG,
                "Android abi is listed in config file and "
                "build for all SOCs plugged in computer, "
                "But no android phone found, "
                "you at least plug in one phone")
        else:
            for soc in target_socs:
                mace_check(
                    soc in available_socs, ModuleName.YAML_CONFIG,
                    "Build specified SOC library, you must plug in a "
                    "phone using the SOC(%s), the current socs: %s" %
                    (soc, available_socs))

    if flags.model_graph_format:
        model_graph_format = flags.model_graph_format
    else:
        model_graph_format = configs.get(YAMLKeyword.model_graph_format, "")
    mace_check(
        model_graph_format in ModelFormatStrs, ModuleName.YAML_CONFIG,
        'You must set model_graph_format and '
        "model_graph_format must be in " + str(ModelFormatStrs))
    configs[YAMLKeyword.model_graph_format] = model_graph_format
    if flags.model_data_format:
        model_data_format = flags.model_data_format
    else:
        model_data_format = configs.get(YAMLKeyword.model_data_format, "")
    configs[YAMLKeyword.model_data_format] = model_data_format
    mace_check(
        model_data_format in ModelFormatStrs, ModuleName.YAML_CONFIG,
        'You must set model_data_format and '
        "model_data_format must be in " + str(ModelFormatStrs))

    mace_check(
        not (model_graph_format == ModelFormat.file
             and model_data_format == ModelFormat.code),
        ModuleName.YAML_CONFIG, "If model_graph format is 'file',"
        " the model_data_format must be 'file' too")

    model_names = configs.get(YAMLKeyword.models, [])
    mace_check(
        len(model_names) > 0, ModuleName.YAML_CONFIG,
        "no model found in config file")

    model_name_reg = re.compile(r'^[a-zA-Z0-9_]+$')
    for model_name in model_names:
        # check model_name legality
        mace_check(model_name not in CPP_KEYWORDS, ModuleName.YAML_CONFIG,
                   "model name should not be c++ keyword.")
        mace_check((model_name[0] == '_' or model_name[0].isalpha())
                   and bool(model_name_reg.match(model_name)),
                   ModuleName.YAML_CONFIG,
                   "model name should Meet the c++ naming convention"
                   " which start with '_' or alpha"
                   " and only contain alpha, number and '_'")

        model_config = configs[YAMLKeyword.models][model_name]
        platform = model_config.get(YAMLKeyword.platform, "")
        mace_check(platform in PlatformTypeStrs, ModuleName.YAML_CONFIG,
                   "'platform' must be in " + str(PlatformTypeStrs))

        for key in [
                YAMLKeyword.model_file_path, YAMLKeyword.model_sha256_checksum
        ]:
            value = model_config.get(key, "")
            mace_check(value != "", ModuleName.YAML_CONFIG,
                       "'%s' is necessary" % key)

        weight_file_path = model_config.get(YAMLKeyword.weight_file_path, "")
        if weight_file_path:
            weight_checksum = \
                model_config.get(YAMLKeyword.weight_sha256_checksum, "")
            mace_check(
                weight_checksum != "", ModuleName.YAML_CONFIG,
                "'%s' is necessary" % YAMLKeyword.weight_sha256_checksum)
        else:
            model_config[YAMLKeyword.weight_sha256_checksum] = ""

        get_model_files(model_config, BUILD_DOWNLOADS_DIR)

        subgraphs = model_config.get(YAMLKeyword.subgraphs, "")
        mace_check(
            len(subgraphs) > 0, ModuleName.YAML_CONFIG,
            "at least one subgraph is needed")

        if isinstance(subgraphs, list):
            graphs_dict = {}
            model_config[YAMLKeyword.input_tensors] = \
                config_parser.to_list(subgraphs[0][YAMLKeyword.input_tensors])
            model_config[YAMLKeyword.output_tensors] = \
                config_parser.to_list(subgraphs[0][YAMLKeyword.output_tensors])
            model_config[YAMLKeyword.validation_inputs_data] = \
                config_parser.to_list(
                    subgraphs[0].get(YAMLKeyword.validation_inputs_data, []))
            model_config[YAMLKeyword.validation_outputs_data] = \
                config_parser.to_list(
                    subgraphs[0].get(YAMLKeyword.validation_outputs_data, []))
            graphs_dict[YAMLKeyword.default_graph] = subgraphs[0]
            subgraphs = graphs_dict
            model_config[YAMLKeyword.subgraphs] = subgraphs

        for graph_name, graph_config in subgraphs.items():
            runtime = DeviceWrapper.get_graph_runtime(graph_config,
                                                      model_config,
                                                      target_abis)
            get_data_type(model_config, runtime)

        input_ranges = model_config.get(YAMLKeyword.input_ranges, [])
        if not isinstance(input_ranges, list):
            model_config[YAMLKeyword.input_ranges] = [input_ranges]
        else:
            model_config[YAMLKeyword.input_ranges] = input_ranges
        model_config[YAMLKeyword.input_ranges] = \
            [str(v) for v in model_config[YAMLKeyword.input_ranges]]

        accuracy_validation_script = model_config.get(
            YAMLKeyword.accuracy_validation_script, "")
        if isinstance(accuracy_validation_script, list):
            mace_check(
                len(accuracy_validation_script) == 1, ModuleName.YAML_CONFIG,
                "Only support one accuracy validation script")
            accuracy_validation_script = accuracy_validation_script[0]
        model_config[YAMLKeyword.accuracy_validation_script] = \
            accuracy_validation_script
        validation_inputs_data = model_config.get(
            YAMLKeyword.validation_inputs_data, [])
        if not isinstance(validation_inputs_data, list):
            model_config[YAMLKeyword.validation_inputs_data] = [
                validation_inputs_data
            ]
        else:
            model_config[YAMLKeyword.validation_inputs_data] = \
                validation_inputs_data

        for key in [YAMLKeyword.check_tensors, YAMLKeyword.check_shapes]:
            value = model_config.get(key, None)
            if value:
                if not isinstance(value, list):
                    model_config[key] = [value]
                model_config[key] = [str(v) for v in subgraph[key]]
            else:
                model_config[key] = []

        validation_threshold = model_config.get(
            YAMLKeyword.validation_threshold, {})
        if not isinstance(validation_threshold, dict):
            raise argparse.ArgumentTypeError(
                'similarity threshold must be a dict.')
        threshold_dict = {
            DeviceType.CPU: ValidationThreshold.cpu_threshold,
            DeviceType.GPU: ValidationThreshold.gpu_threshold,
            DeviceType.HEXAGON: ValidationThreshold.quantize_threshold,
            DeviceType.HTA: ValidationThreshold.quantize_threshold,
            DeviceType.APU: ValidationThreshold.quantize_threshold,
            DeviceType.QUANTIZE: ValidationThreshold.quantize_threshold,
        }
        for k, v in six.iteritems(validation_threshold):
            if k.upper() == 'DSP':
                k = DeviceType.HEXAGON
            if k.upper() not in (DeviceType.CPU, DeviceType.GPU,
                                 DeviceType.HEXAGON, DeviceType.HTA,
                                 DeviceType.QUANTIZE):
                raise argparse.ArgumentTypeError(
                    'Unsupported validation threshold runtime: %s' % k)
            threshold_dict[k.upper()] = v
        model_config[YAMLKeyword.validation_threshold] = threshold_dict

        model_backend = model_config.get(YAMLKeyword.backend, "tensorflow")
        model_config[YAMLKeyword.backend] = model_backend
        validation_outputs_data = model_config.get(
            YAMLKeyword.validation_outputs_data, [])
        if not isinstance(validation_outputs_data, list):
            model_config[YAMLKeyword.validation_outputs_data] = [
                validation_outputs_data
            ]
        else:
            model_config[YAMLKeyword.validation_outputs_data] = \
                validation_outputs_data

        for (subname, subgraph) in subgraphs.items():
            for key in [
                    YAMLKeyword.input_tensors, YAMLKeyword.input_shapes,
                    YAMLKeyword.output_tensors, YAMLKeyword.output_shapes
            ]:
                value = subgraph.get(key, "")
                mace_check(value != "", ModuleName.YAML_CONFIG,
                           "'%s' is necessary in %s" % (key, subname))
                if not isinstance(value, list):
                    subgraph[key] = [value]
                subgraph[key] = [str(v) for v in subgraph[key]]
                # --inputs_shapes will be passed to ELF file `mace_run_static',
                # if input_shapes contains spaces, such as: '1, 3, 224, 224',
                # because mace_run.cc use gflags to parse command line
                # arguments, --input_shapes 1, 3, 224, 224 will be passed as
                # `--input_shapes 1,'. So we strip out spaces here.
                if key in [
                        YAMLKeyword.input_shapes, YAMLKeyword.output_shapes
                ]:
                    subgraph[key] = [e.replace(' ', '') for e in subgraph[key]]
            subgraph[YAMLKeyword.input_tensors] = \
                config_parser.to_list(subgraph[YAMLKeyword.input_tensors])
            input_size = len(subgraph[YAMLKeyword.input_tensors])
            subgraph[YAMLKeyword.output_tensors] = \
                config_parser.to_list(subgraph[YAMLKeyword.output_tensors])
            output_size = len(subgraph[YAMLKeyword.output_tensors])

            mace_check(
                len(subgraph[YAMLKeyword.input_shapes]) == input_size,
                ModuleName.YAML_CONFIG,
                "input shapes' size not equal inputs' size.")
            mace_check(
                len(subgraph[YAMLKeyword.output_shapes]) == output_size,
                ModuleName.YAML_CONFIG,
                "output shapes' size not equal outputs' size.")

            for key in [
                    YAMLKeyword.input_data_types, YAMLKeyword.output_data_types
            ]:
                if key == YAMLKeyword.input_data_types:
                    count = input_size
                else:
                    count = output_size
                data_types = subgraph.get(key, "")
                if data_types:
                    if not isinstance(data_types, list):
                        subgraph[key] = [data_types] * count
                    for data_type in subgraph[key]:
                        mace_check(
                            data_type in InOutDataTypeStrs,
                            ModuleName.YAML_CONFIG,
                            key + " must be in " + str(InOutDataTypeStrs))
                else:
                    subgraph[key] = [InOutDataType.float32] * count

            input_data_formats = \
                subgraph.get(YAMLKeyword.input_data_formats, [])
            if input_data_formats:
                if not isinstance(input_data_formats, list):
                    subgraph[YAMLKeyword.input_data_formats] = \
                        [input_data_formats] * input_size
                else:
                    mace_check(
                        len(input_data_formats) == input_size,
                        ModuleName.YAML_CONFIG,
                        "input_data_formats should match"
                        " the size of input.")
                for input_data_format in \
                        subgraph[YAMLKeyword.input_data_formats]:
                    mace_check(
                        input_data_format in DataFormatStrs,
                        ModuleName.YAML_CONFIG,
                        "'input_data_formats' must be in " +
                        str(DataFormatStrs) + ", but got " + input_data_format)
            else:
                subgraph[YAMLKeyword.input_data_formats] = \
                    [DataFormat.NHWC] * input_size

            output_data_formats = subgraph.get(YAMLKeyword.output_data_formats,
                                               [])
            if output_data_formats:
                if not isinstance(output_data_formats, list):
                    subgraph[YAMLKeyword.output_data_formats] = \
                        [output_data_formats] * output_size
                else:
                    mace_check(
                        len(output_data_formats) == output_size,
                        ModuleName.YAML_CONFIG,
                        "output_data_formats should match"
                        " the size of output")
                for output_data_format in \
                        subgraph[YAMLKeyword.output_data_formats]:
                    mace_check(
                        output_data_format in DataFormatStrs,
                        ModuleName.YAML_CONFIG,
                        "'output_data_formats' must be in " +
                        str(DataFormatStrs))
            else:
                subgraph[YAMLKeyword.output_data_formats] = \
                    [DataFormat.NHWC] * output_size

        for key in [
                YAMLKeyword.limit_opencl_kernel_time,
                YAMLKeyword.opencl_queue_window_size,
                YAMLKeyword.nnlib_graph_mode, YAMLKeyword.obfuscate,
                YAMLKeyword.winograd, YAMLKeyword.quantize,
                YAMLKeyword.quantize_large_weights,
                YAMLKeyword.change_concat_ranges
        ]:
            value = model_config.get(key, "")
            if value == "":
                model_config[key] = 0

        mace_check(
            model_config[YAMLKeyword.quantize] == 0
            or model_config[YAMLKeyword.quantize_large_weights] == 0,
            ModuleName.YAML_CONFIG,
            "quantize and quantize_large_weights should not be set to 1"
            " at the same time.")

        mace_check(
            model_config[YAMLKeyword.winograd] in WinogradParameters,
            ModuleName.YAML_CONFIG, "'winograd' parameters must be in " +
            str(WinogradParameters) + ". 0 for disable winograd convolution")

    return configs