code: def run_model_for_device(flags, args, dev, model_name, model_conf):
      target_abi = flags.target_abi
      install_dir = (
          run_target.default_install_dir(target_abi) + "/" + model_name
      )
      sysdir = install_dir + "/interior"
      dev.mkdir(sysdir)
  
      runtime_list = []
      for graph_name, graph_conf in model_conf[
          ModelKeys.subgraphs
      ].items():
          runtime = graph_conf[ModelKeys.runtime]
          runtime_list.append(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
      model_file = model_name + ".pb"
      model_data_file = model_name + ".data"
      model_path = workdir + "/model/" + model_file
      model_data_path = workdir + "/model/" + model_data_file
      if os.path.exists(model_path) and os.path.exists(model_data_path):
          dev.install(Target(model_path), install_dir)
          dev.install(Target(model_data_path), install_dir)
      else:
          MaceLogger.warning(
              "No models exist in %s, use --model_file and"
              " --model_data_file specified in args" % model_path
          )
  
      if ModelKeys.check_tensors in model_conf:
          model_conf[ModelKeys.output_tensors] = model_conf[
              ModelKeys.check_tensors
          ]
          model_conf[ModelKeys.output_shapes] = model_conf[
              ModelKeys.check_shapes
          ]
  
      model_file_path = ""
      if not flags.gencode_model:
          model_file_path = install_dir + "/" + model_file
      model_data_file_path = ""
      if not flags.gencode_param:
          model_data_file_path = install_dir + "/" + model_data_file
  
      input_tensors_info = config_parser.find_input_tensors_info(
          model_conf[ModelKeys.subgraphs],
          model_conf[ModelKeys.input_tensors],
      )
      output_tensors_info = config_parser.find_output_tensors_info(
          model_conf[ModelKeys.subgraphs],
          model_conf[ModelKeys.output_tensors],
      )
  
      model_args = {
          "model_name": model_name,
          "model_file": model_file_path,
          "model_data_file": model_data_file_path,
          "input_node": ",".join(model_conf[ModelKeys.input_tensors]),
          "input_shape": join_2d_array(
              input_tensors_info[ModelKeys.input_shapes]
          ),
          "output_node": ",".join(model_conf[ModelKeys.output_tensors]),
          "output_shape": join_2d_array(
              output_tensors_info[ModelKeys.output_shapes]
          ),
          "input_data_format": ",".join(
              [
                  df.name
                  for df in input_tensors_info[
                      ModelKeys.input_data_formats
                  ]
              ]
          ),
          "output_data_format": ",".join(
              [
                  df.name
                  for df in output_tensors_info[
                      ModelKeys.output_data_formats
                  ]
              ]
          ),
      }
  
      opts = [
          "--%s='%s'" % (arg_key, arg_val)
          for arg_key, arg_val in model_args.items()
      ] + args
      should_generate_data = (
          flags.validate
          or flags.quantize_stat
          or flags.tune
          or "--benchmark" in opts
      )
  
      if should_generate_data:
          tmpdirname = tempfile.mkdtemp()
          input_file_prefix = tmpdirname + "/" + model_name
  
          if ModelKeys.validation_inputs_data in model_conf:
              input_tensor = model_conf[ModelKeys.input_tensors]
              input_data = model_conf[ModelKeys.validation_inputs_data]
              mace_check(
                  len(input_tensor) == len(input_data),
                  "len(input_tensor) != len(validate_data",
              )
  
              for i in range(len(input_tensor)):
                  util.download_or_get_file(
                      model_conf[ModelKeys.validation_inputs_data][i],
                      "",
                      util.formatted_file_name(
                          input_file_prefix, input_tensor[i]
                      ),
                  )
          else:
              generate_input_data(
                  input_file_prefix,
                  model_conf[ModelKeys.input_tensors],
                  input_tensors_info[ModelKeys.input_shapes],
                  input_tensors_info[ModelKeys.input_ranges],
                  input_tensors_info[ModelKeys.input_data_types],
              )
  
          dev.install(Target(tmpdirname), install_dir + "/validate_in")
          target_input_file = "%s/validate_in/%s" % (
              install_dir,
              model_name,
          )
          target_output_dir = "%s/validate_out" % install_dir
          dev.mkdir(target_output_dir)
          target_output_file = target_output_dir + "/" + model_name
          opts += [
              "--input_file=%s" % target_input_file,
              "--output_file=%s" % target_output_file,
          ]
  
      # run
      envs = flags.envs.split(" ") + [
          "MACE_INTERNAL_STORAGE_PATH=%s" % sysdir
      ]
      if flags.tune:
          envs += [
              "MACE_TUNING=1",
              "MACE_RUN_PARAMETER_PATH=%s/interior/tune_params"
              % install_dir,
          ]
          opts += ["--round=0"]
  
      mace_check(
          flags.vlog_level >= 0,
          "vlog_level should be greater than zeror",
      )
      envs += ["MACE_CPP_MIN_VLOG_LEVEL=%s" % flags.vlog_level]
      if flags.quantize_stat:
          envs += ["MACE_LOG_TENSOR_RANGE=1"]
  
      build_dir = flags.build_dir + "/" + target_abi
      libs = []
      if DeviceType.HEXAGON in runtime_list:
          libs += [
              "third_party/nnlib/%s/libhexagon_controller.so"
              % target_abi
          ]
      elif runtime == DeviceType.HTA:
          libs += [
              "third_party/hta/%s/libhta_hexagon_runtime.so"
              % target_abi
          ]
      elif DeviceType.APU in runtime_list:
          apu_libs = get_apu_so_paths(dev)
          libs += apu_libs
      elif DeviceType.HTP in runtime_list:
          libs += [
              "third_party/qnn/target/%s/libQnnHtpAltPrepStub.so"
              % target_abi
          ]
          libs += [
              "third_party/qnn/target/%s/libQnnHtpAltPrepV69Stub.so"
              % target_abi
          ]
      cpp_shared_lib_path = os.path.join(
          build_dir, "install/lib/libc++_shared.so"
      )
      if os.path.exists(cpp_shared_lib_path):
          libs.append(cpp_shared_lib_path)
  
      target = Target(
          build_dir + "/install/bin/mace_run",
          libs,
          opts=opts,
          envs=envs,
      )
      run_target.run_target(target_abi, install_dir, target, dev)
  
      if DeviceType.GPU in runtime_list:
          opencl_dir = workdir + "/opencl"
          util.mkdir_p(opencl_dir)
          dev.pull(
              Target(
                  install_dir + "/interior/mace_cl_compiled_program.bin"
              ),
              "%s/%s_compiled_opencl_kernel.%s.%s.bin"
              % (
                  opencl_dir,
                  model_name,
                  dev.info()["ro.product.model"].replace(" ", ""),
                  dev.info()["ro.board.platform"],
              ),
          )
          if flags.tune:
              dev.pull(
                  Target(install_dir + "/interior/tune_params"),
                  "%s/%s_tuned_opencl_parameter.%s.%s.bin"
                  % (
                      opencl_dir,
                      model_name,
                      dev.info()["ro.product.model"].replace(" ", ""),
                      dev.info()["ro.board.platform"],
                  ),
              )
  
      if flags.validate:
          validate_model_file = util.download_or_get_model(
              model_conf[ModelKeys.model_file_path],
              model_conf[ModelKeys.model_sha256_checksum],
              tmpdirname,
          )
  
          validate_weight_file = ""
          if ModelKeys.weight_file_path in model_conf:
              validate_weight_file = util.download_or_get_model(
                  model_conf[ModelKeys.weight_file_path],
                  model_conf[ModelKeys.weight_sha256_checksum],
                  tmpdirname,
              )
  
          dev.pull(
              Target(target_output_dir), tmpdirname + "/validate_out"
          )
          output_file_prefix = (
              tmpdirname + "/validate_out/" + model_name
          )
          validation_outputs_data = model_conf.get(
              ModelKeys.validation_outputs_data, []
          )
          validate.validate(
              model_conf[ModelKeys.platform],
              validate_model_file,
              validate_weight_file,
              input_file_prefix,
              output_file_prefix,
              input_tensors_info[ModelKeys.input_shapes],
              output_tensors_info[ModelKeys.output_shapes],
              input_tensors_info[ModelKeys.input_data_formats],
              output_tensors_info[ModelKeys.output_data_formats],
              input_tensors_info[ModelKeys.input_tensors],
              output_tensors_info[ModelKeys.output_tensors],
              flags.validate_threshold,
              input_tensors_info[ModelKeys.input_data_types],
              flags.backend,
              validation_outputs_data,
              "",
          )
      if should_generate_data:
          shutil.rmtree(tmpdirname)
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import argparse
  import os
  import tempfile
  import shutil
  import numpy
  from apu_utils import get_apu_so_paths
  from py_proto import mace_pb2
  from utils import util
  from utils import device
  from utils import config_parser
  from utils.config_parser import DeviceType
  from utils.target import Target
  from utils.config_parser import ModelKeys
  from utils.util import ABIType
  from utils.util import MaceLogger
  from utils.util import mace_check
  import run_target
  import validate
label_desc: Use image node_id 5 for calling a global function with example usage: run_model_for_device(flags, args, dev, model_name, model_conf) without return types