def prepare_data(opts): """Build auxiliary data file.""" # imagenet synset synset_url = ''.join([ 'https://gist.githubusercontent.com/zhreshold/', '4d0b62f3d01426887599d4f7ede23ee5/raw/', '596b27d23537e5a1b5751d2b0481ef172f58b539/', 'imagenet1000_clsid_to_human.txt' ]) synset_name = 'imagenet1000_clsid_to_human.txt' synset_path = download_testdata(synset_url, synset_name, module='data') with open(synset_path) as f: synset = eval(f.read()) build_dir = opts.out_dir with open(os.path.join(build_dir, "imagenet1k_synset.json"), "w") as f_synset: f_synset.write(json.dumps(synset)) # Test image image_url = "https://homes.cs.washington.edu/~moreau/media/vta/cat.jpg" image_fn = os.path.join(build_dir, "cat.png") download_testdata(image_url, image_fn) # copy runtime js files. shutil.copyfile( libinfo.find_lib_path("tvmjs.bundle.js")[0], os.path.join(build_dir, "tvmjs.bundle.js")) shutil.copyfile( libinfo.find_lib_path("tvmjs_runtime.wasi.js")[0], os.path.join(build_dir, "tvmjs_runtime.wasi.js")) shutil.copyfile(os.path.join(curr_dir, "index.html"), os.path.join(build_dir, "index.html"))
def get_configs_json_dir() -> str: """Find the 'configs' directory, containing the JSON files used to configure tvmc with persistent argument settings. Returns ------- str : The path to the 'configs' directory """ global CONFIGS_JSON_DIR if CONFIGS_JSON_DIR is None: candidate_paths = [] candidate_paths.extend(libinfo.find_lib_path()) # When running from source, the configs directory will be located one directory above the # native libraries, so covering that case. candidate_paths.extend([ os.path.abspath(os.path.join(lib_path, "..")) for lib_path in libinfo.find_lib_path() ]) for path in candidate_paths: configs_path = os.path.join(os.path.dirname(path), "configs") if os.path.isdir(configs_path): CONFIGS_JSON_DIR = configs_path break else: raise ConfigsJsonNotFoundError() return CONFIGS_JSON_DIR
def create_tvmjs_wasm(output, objects, options=None, cc="emcc"): """Create wasm that is supposed to run with the tvmjs. Parameters ---------- output : str The target shared library. objects : list List of object files. options : str The additional options. cc : str, optional The compile string. """ cmd = [cc] cmd += ["-O3"] cmd += ["-std=c++17"] cmd += ["--no-entry"] cmd += ["-s", "ERROR_ON_UNDEFINED_SYMBOLS=0"] cmd += ["-s", "STANDALONE_WASM=1"] cmd += ["-s", "ALLOW_MEMORY_GROWTH=1"] objects = [objects] if isinstance(objects, str) else objects with_runtime = False for obj in objects: if obj.find("wasm_runtime.bc") != -1: with_runtime = True if not with_runtime: objects += [find_lib_path("wasm_runtime.bc")[0]] objects += [find_lib_path("tvmjs_support.bc")[0]] objects += [find_lib_path("webgpu_runtime.bc")[0]] cmd += ["-o", output] cmd += objects if options: cmd += options proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) (out, _) = proc.communicate() if proc.returncode != 0: msg = "Compilation error:\n" msg += py_str(out) raise RuntimeError(msg)
def with_minrpc(compile_func, server="posix_popen_server", runtime="libtvm"): """Attach the compiler function with minrpc related options. Parameters ---------- compile_func : Union[str, Callable[[str, str, Optional[str]], None]] The compilation function to decorate. server : str The server type. runtime : str The runtime library. Returns ------- fcompile : function The return compilation. """ server_path = find_minrpc_server_libpath(server) runtime_path = libinfo.find_lib_path([runtime, runtime + ".so", runtime + ".dylib"])[0] runtime_dir = os.path.abspath(os.path.dirname(runtime_path)) options = ["-std=c++14"] # Make sure the rpath to the libtvm is set so we can do local tests. # Note that however, this approach won't work on remote. # Always recommend to to link statically. options += ["-Wl,-rpath=" + runtime_dir] options += ["-I" + path for path in libinfo.find_include_path()] fcompile = cc.cross_compiler( compile_func, options=options, add_files=[server_path, runtime_path] ) fcompile.__name__ = "with_minrpc" fcompile.need_system_lib = True return fcompile
def _load_platform_specific_library(lib_name): system = platform.system() if system == "Darwin": lib_file_name = lib_name + ".dylib" elif system == "Windows": lib_file_name = lib_name + ".dll" else: lib_file_name = lib_name + ".so" lib_path = libinfo.find_lib_path()[0] lib_dir = os.path.dirname(lib_path) lib_file_path = os.path.join(lib_dir, lib_file_name) try: torch.classes.load_library(lib_file_path) except OSError as err: errmsg = str(err) if errmsg.find("undefined symbol") != -1: reason = " ".join( ( "Got undefined symbol error,", "which might be due to the CXXABI incompatibility.", ) ) else: reason = errmsg warnings.warn( f"The library {lib_name} is not built successfully. {reason}", RuntimeWarning, )
def _server_env(load_library, work_path=None): """Server environment function return temp dir""" if work_path: temp = work_path else: temp = utils.tempdir() # pylint: disable=unused-variable @tvm._ffi.register_func("tvm.rpc.server.workpath", override=True) def get_workpath(path): return temp.relpath(path) @tvm._ffi.register_func("tvm.rpc.server.load_module", override=True) def load_module(file_name): """Load module from remote side.""" path = temp.relpath(file_name) m = _load_module(path) logger.info("load_module %s", path) return m @tvm._ffi.register_func("tvm.rpc.server.download_linked_module", override=True) def download_linked_module(file_name): """Load module from remote side.""" # c++ compiler/linker cc = os.environ.get("CXX", "g++") # pylint: disable=import-outside-toplevel path = temp.relpath(file_name) if path.endswith(".o"): # Extra dependencies during runtime. from tvm.contrib import cc as _cc _cc.create_shared(path + ".so", path, cc=cc) path += ".so" elif path.endswith(".tar"): # Extra dependencies during runtime. from tvm.contrib import cc as _cc, tar as _tar tar_temp = utils.tempdir(custom_path=path.replace(".tar", "")) _tar.untar(path, tar_temp.temp_dir) files = [tar_temp.relpath(x) for x in tar_temp.listdir()] _cc.create_shared(path + ".so", files, cc=cc) path += ".so" elif path.endswith(".dylib") or path.endswith(".so"): pass else: raise RuntimeError("Do not know how to link %s" % file_name) logger.info("Send linked module %s to client", path) return bytearray(open(path, "rb").read()) libs = [] load_library = load_library.split(":") if load_library else [] for file_name in load_library: file_name = find_lib_path(file_name)[0] libs.append(ctypes.CDLL(file_name, ctypes.RTLD_GLOBAL)) logger.info("Load additional library %s", file_name) temp.libs = libs return temp
def _server_env(load_library, work_path=None): """Server environment function return temp dir""" if work_path: temp = work_path else: temp = util.tempdir() # pylint: disable=unused-variable @tvm._ffi.register_func("tvm.rpc.server.workpath", override=True) def get_workpath(path): return temp.relpath(path) @tvm._ffi.register_func("tvm.rpc.server.load_module", override=True) def load_module(file_name): """Load module from remote side.""" path = temp.relpath(file_name) m = _load_module(path) logger.info("load_module %s", path) return m libs = [] load_library = load_library.split(":") if load_library else [] for file_name in load_library: file_name = find_lib_path(file_name)[0] libs.append(ctypes.CDLL(file_name, ctypes.RTLD_GLOBAL)) logger.info("Load additional library %s", file_name) temp.libs = libs return temp
def _load_lib(): """Load libary by searching possible path.""" lib_path = [] pwd = os.path.dirname(os.path.realpath(__file__)) paths = [ os.path.realpath(pwd + "/../../lib"), os.path.realpath(pwd + "/../../../../../mindspore/lib") ] tar_so = "libakg.so" for path in paths: found_lib = False if os.path.exists(path): files = os.listdir(path) for f in files: if f == tar_so: lib_path.append(path + "/" + f) found_lib = True break if found_lib: break if not lib_path: lib_path = libinfo.find_lib_path() if not lib_path: raise RuntimeError("Cannot find library {}.".format(tar_so)) lib = ctypes.CDLL(lib_path[0], ctypes.RTLD_GLOBAL) return lib, os.path.basename(lib_path[0])
def _load_lib(): """Load libary by searching possible path.""" curr_path = os.path.dirname(os.path.realpath(os.path.expanduser(__file__))) lib_search = curr_path lib_path = libinfo.find_lib_path(_get_lib_names(), lib_search, optional=True) if lib_path is None: return None, None lib = ctypes.CDLL(lib_path[0], ctypes.RTLD_GLOBAL) return lib, os.path.basename(lib_path[0])
def _load_lib(): """Load libary by searching possible path.""" curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__))) lib_search = curr_path lib_path = libinfo.find_lib_path(_get_lib_names(), lib_search, optional=True) if lib_path is None: return None, None lib = ctypes.CDLL(lib_path[0], ctypes.RTLD_GLOBAL) return lib, os.path.basename(lib_path[0])
def _load_platform_specific_library(lib_name="libpt_tvmdsoop"): system = platform.system() if system == "Darwin": lib_file_name = lib_name + ".dylib" elif system == "Windows": lib_file_name = lib_name + ".dll" else: lib_file_name = lib_name + ".so" lib_path = libinfo.find_lib_path()[0] lib_dir = os.path.dirname(lib_path) lib_file_path = os.path.join(lib_dir, lib_file_name) torch.classes.load_library(lib_file_path)
def get_configs_json_dir() -> str: """Find the 'configs' directory, containing the JSON files used to configure tvmc with persistent argument settings. Returns ------- str : The path to the 'configs' directory """ global CONFIGS_JSON_DIR if CONFIGS_JSON_DIR is None: # If a non-default location for the build directory is used, e.g. set via TVM_LIBRARY_PATH # we need to provide the user a way to overwrite CONFIGS_JSON_DIR as well. if os.environ.get("TVM_CONFIGS_JSON_DIR", None): user_config_dir = os.environ["TVM_CONFIGS_JSON_DIR"] if os.path.isdir(user_config_dir): CONFIGS_JSON_DIR = user_config_dir return CONFIGS_JSON_DIR candidate_paths = [] candidate_paths.extend(libinfo.find_lib_path()) # When running from source, the configs directory will be located one directory above the # native libraries, so covering that case. candidate_paths.extend([ os.path.abspath(os.path.join(lib_path, "..")) for lib_path in libinfo.find_lib_path() ]) for path in candidate_paths: configs_path = os.path.join(os.path.dirname(path), "configs") if os.path.isdir(configs_path): CONFIGS_JSON_DIR = configs_path break else: raise ConfigsJsonNotFoundError() return CONFIGS_JSON_DIR