def run_layers_validate(flags, args, original_conf): model_name = flags.model_name original_model_dir = flags.output + "/" + \ original_conf['library_name'] + "/model" model_dir = "/tmp/micro_run/model" device.execute("mkdir -p %s" % model_dir) device.execute("cp -p %s/%s.pb %s" % (original_model_dir, model_name, model_dir)) params_file_path = "%s/%s.data" % (original_model_dir, model_name) output_configs = layers_validate.get_layers(model_dir, model_name, flags.layers) for i in range(len(output_configs)): sub_model_conf = gen_sub_model_conf(output_configs[i], flags, original_conf) with open(output_configs[i]['model_file_path'], "rb") as model_file: net_def = mace_pb2.NetDef() net_def.ParseFromString(model_file.read()) with open(params_file_path, "rb") as params_file: weights = bytearray(params_file.read()) micro_conf = \ config_parser.normalize_model_config(sub_model_conf) MicroConverter(micro_conf, net_def, weights, model_name).gen_code() build_engine(model_name, micro_conf[ModelKeys.data_type]) run_model_with_conf(flags, args, model_name, micro_conf)
def copy_so_files(flags): apu_so_paths = [] device_id = get_cur_device_id(flags) if device_id is not None: android_device = device.create_device(flags.target_abi, device_id) apu_so_paths = get_apu_so_paths(android_device) for apu_so_path in apu_so_paths: device.execute("cp -f %s %s" % (apu_so_path, flags.apu_path), True)
def build_engine(flags): cmake_shell = os.path.abspath(os.path.dirname( __file__)) + "/../cmake/cmake-build-%s.sh" % flags.target_abi os.environ["BUILD_DIR"] = flags.build_dir + "/" + flags.target_abi if flags.runtime: os.environ["RUNTIME"] = config_parser.parse_device_type( flags.runtime).name if flags.gencode_model: os.environ["RUNMODE"] = "code" device.execute("bash " + cmake_shell)
def build_engine(model_name, data_type): mace_check(flags.model_name is not None and len(model_name) > 0, "you should specify model name for build.") command = "bazel build //micro/tools:micro_run_static" \ " --config optimization " \ " --copt \"-DMICRO_MODEL_NAME=%s\"" % model_name if data_type == mace_pb2.DT_BFLOAT16: command += " --copt \"-DMACE_ENABLE_BFLOAT16\"" print("The current engine's data type is bfloat16.") device.execute(command)
def build_engine(model_name, data_type): mace_check(flags.model_name is not None and len(model_name) > 0, "you should specify model name for build.") command = ("micro/tools/cmake/cmake-build-host.sh" " -DMICRO_MODEL_NAME=%s -DMACE_MICRO_ENABLE_CMSIS=ON" " -DCMAKE_BUILD_TYPE=Release" % model_name) if data_type == mace_pb2.DT_BFLOAT16: command += " -DMACE_MICRO_ENABLE_BFLOAT16=ON" print("The current engine's data type is bfloat16.") else: command += " -DMACE_MICRO_ENABLE_BFLOAT16=OFF" device.execute(command)
def download_or_get_file(file, sha256_checksum, output_dir): filename = os.path.basename(file) output_file = "%s/%s-%s.pb" % (output_dir, filename, sha256_checksum) if file.startswith("http://") or file.startswith("https://"): if not os.path.exists( output_file) or file_checksum(output_file) != sha256_checksum: MaceLogger.info("Downloading file %s, please wait ..." % file) urllib.urlretrieve(file, output_file) MaceLogger.info("Model downloaded successfully.") else: device.execute("cp %s %s" % (file, output_file)) return output_file
# See the License for the specific language governing permissions and # limitations under the License. from __future__ import absolute_import from __future__ import division from __future__ import print_function import os from utils import device from utils.util import MaceLogger cwd = os.path.dirname(__file__) # TODO: Remove bazel deps try: device.execute("bazel version") except: # noqa MaceLogger.warning("No bazel, use cmake.") device.execute("bash tools/cmake/cmake-generate-proto-py-host.sh") else: try: device.execute("bazel build //mace/proto:mace_py") device.execute("cp -f bazel-genfiles/mace/proto/mace_pb2.py %s" % cwd) device.execute("bazel build //mace/proto:micro_mem_py") device.execute("cp -f bazel-genfiles/mace/proto/micro_mem_pb2.py %s" % cwd) device.execute("bazel build //third_party/caffe:caffe_py") device.execute( "cp -f bazel-genfiles/third_party/caffe/caffe_pb2.py %s" % cwd)
# You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import absolute_import from __future__ import division from __future__ import print_function import os from utils import device from utils.util import MaceLogger cwd = os.path.dirname(__file__) # TODO: Remove bazel deps try: device.execute("bazel build //mace/proto:mace_py") device.execute("cp -f bazel-genfiles/mace/proto/mace_pb2.py %s" % cwd) device.execute("bazel build //third_party/caffe:caffe_py") device.execute("cp -f bazel-genfiles/third_party/caffe/caffe_pb2.py %s" % cwd) except: # noqa MaceLogger.warning("No bazel, use cmake.")
" MACE_INTERNAL_STORAGE_PATH=/path/to," " LD_PRELOAD=/path/to") flgs, args = parser.parse_known_args() return flgs, args if __name__ == "__main__": flags, args = parse_args() if flags.device_conf: device_conf = config_parser.parse_device_info(flags.device_conf) device.ArmLinuxDevice.set_devices(device_conf) target_abi = flags.target_abi.strip() target_name = flags.target_name.strip() envs = flags.envs.split(" ") # build build_dir = flags.build_dir + "/" + target_abi if flags.build: cmake_shell = os.path.abspath(os.path.dirname( __file__)) + "/../cmake/cmake-build-%s.sh" % target_abi os.environ["BUILD_DIR"] = build_dir device.execute("bash " + cmake_shell) # run target = target.Target(build_dir + "/install/bin/" + target_name, opts=args, envs=envs) run_target(target_abi, None, target, device_ids=flags.target_socs)
" MACE_INTERNAL_STORAGE_PATH=/path/to," " LD_PRELOAD=/path/to") flgs, args = parser.parse_known_args() return flgs, args if __name__ == "__main__": flags, args = parse_args() if flags.device_conf: device_conf = config_parser.parse_device_info(flags.device_conf) device.ArmLinuxDevice.set_devices(device_conf) target_abi = flags.target_abi.strip() target_name = flags.target_name.strip() envs = flags.envs.split(" ") # build build_dir = flags.build_dir + "/" + target_abi if flags.build: cmake_shell = os.path.abspath( os.path.dirname( __file__)) + "/../cmake/cmake-build-%s.sh" % target_abi os.environ["BUILD_DIR"] = build_dir device.execute("RUNTIME=GPU QUANTIZE=ON bash " + cmake_shell) # run target = target.Target(build_dir + "/install/bin/" + target_name, opts=args, envs=envs) run_target_origin(target_abi, None, target, device_ids=flags.target_socs)
default="arm64-v8a", help="Target ABI: only support arm64-v8a") parser.add_argument("--target_soc", type=str, default="all", help="serialno for adb connection") parser.add_argument("--apu_path", type=str, default="", help="path for storing apu so files on device") return parser.parse_known_args() if __name__ == "__main__": flags, args = parse_args() run_devices = device.choose_devices(flags.target_abi, flags.target_soc) device_num = len(run_devices) if device_num == 0: # for CI MaceLogger.warning("No Android devices are plugged in, " "you need to copy `apu` so files by yourself.") elif device_num > 1: # for CI MaceLogger.warning("More than one Android devices are plugged in, " "you need to copy `apu` so files by yourself.") else: device_id = run_devices[0] android_device = device.create_device(flags.target_abi, device_id) apu_so_paths = get_apu_so_paths(android_device) for apu_so_path in apu_so_paths: device.execute("cp -f %s %s" % (apu_so_path, flags.apu_path), True)
" LD_PRELOAD=/path/to") flgs, _ = parser.parse_known_args() return flgs if __name__ == "__main__": flags = parse_args() if flags.device_conf: device_conf = config_parser.parse_device_info(flags.device_conf) device.ArmLinuxDevice.set_devices(device_conf) target_abi = flags.target_abi.strip() target_name = flags.target_name.strip() opts = flags.args.split(" ") envs = flags.envs.split(" ") # build build_dir = flags.build_dir + "/" + target_abi if flags.build: cmake_shell = os.path.abspath( os.path.dirname( __file__)) + "/config/build/cmake-build-%s.sh" % target_abi os.environ["BUILD_DIR"] = build_dir device.execute(cmake_shell) # run target = target.Target(build_dir + "/install/bin/" + target_name, opts=opts, envs=envs) run_target(target_abi, None, target, device_ids=flags.target_socs)
trim_blocks=True) # generate mace_run BUILD file template_name = 'mace_engine_factory.h.jinja2' model_name = list(model_name) source = j2_env.get_template(template_name).render( model_tags=model_name, embed_model_data=embed_model_data, ) with open(output + '/mace_engine_factory.h', "w") as f: f.write(source) if __name__ == '__main__': flags = parse_args() codegen_dir = "mace/codegen/models" device.execute("rm -rf %s/*" % codegen_dir) models = [] if flags.config: conf = config_parser.parse(flags.config) for name, model_conf in conf["models"].items(): model_conf = config_parser.normalize_model_config(model_conf) if not flags.model_name or name == flags.model_name: MaceLogger.info("Encrypt model %s" % name) encrypt(name, "build/%s/model/%s.pb" % (name, name), "build/%s/model/%s.data" % (name, name), model_conf[ModelKeys.runtime], codegen_dir + "/" + name, not flags.no_obfuscate, flags.gencode_model, flags.gencode_param) models.append(name)