Running TVM on FreeBSD

Here is a patch to make tvm runtime working under FreeBSD. Compiling works ok (including OpenCL) but runtime .py needs little adjustments to get the compiler name and library search path. As a note simple .so (i.e. CPU only) compiled on Linux work on FreeBSD …

From 69850f072e2ca2d4d11f49f2acb8eca93f5ac00a Mon Sep 17 00:00:00 2001
From: Mihai M <mmihai@delajii.net>
Date: Sun, 27 Sep 2020 16:18:46 -0700
Subject: [PATCH] Updated runtime to run under FreeBSD - tested w/ 12.0, g++9.

---
 python/tvm/_ffi/libinfo.py   |  2 +-
 python/tvm/contrib/cc.py     |  4 ++--
 python/tvm/rpc/server.py     |  7 +++++++
 python/tvm/runtime/module.py | 13 +++++++++++--
 4 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/python/tvm/_ffi/libinfo.py b/python/tvm/_ffi/libinfo.py
index b9fc8dc55..ae0f63ea4 100644
--- a/python/tvm/_ffi/libinfo.py
+++ b/python/tvm/_ffi/libinfo.py
@@ -70,7 +70,7 @@ def find_lib_path(name=None, search_path=None, optional=False):
     if os.environ.get("TVM_LIBRARY_PATH", None):
         dll_path.append(os.environ["TVM_LIBRARY_PATH"])
 
-    if sys.platform.startswith("linux"):
+    if sys.platform.startswith("linux") or sys.platform.startswith("freebsd"):
         dll_path.extend(split_env_var("LD_LIBRARY_PATH", ":"))
         dll_path.extend(split_env_var("PATH", ":"))
     elif sys.platform.startswith("darwin"):
diff --git a/python/tvm/contrib/cc.py b/python/tvm/contrib/cc.py
index 1b6a62fd9..61ccf48a8 100644
--- a/python/tvm/contrib/cc.py
+++ b/python/tvm/contrib/cc.py
@@ -39,7 +39,7 @@ def create_shared(output, objects, options=None, cc="g++"):
     cc : Optional[str]
         The compiler command.
     """
-    if sys.platform == "darwin" or sys.platform.startswith("linux"):
+    if sys.platform == "darwin" or sys.platform.startswith("linux") or sys.platform.startswith("freebsd"):
         _linux_compile(output, objects, options, cc, compile_shared=True)
     elif sys.platform == "win32":
         _windows_shared(output, objects, options)
@@ -103,7 +103,7 @@ def get_target_by_dump_machine(compiler):
 # assign so as default output format
 create_shared.output_format = "so" if sys.platform != "win32" else "dll"
 create_shared.get_target_triple = get_target_by_dump_machine(
-    "g++" if sys.platform == "darwin" or sys.platform.startswith("linux") else None
+    "g++" if sys.platform == "darwin" or sys.platform.startswith("linux") else "g++9" if sys.platform.startswith("freebsd") else None
 )
 
 
diff --git a/python/tvm/rpc/server.py b/python/tvm/rpc/server.py
index 03a124b46..eef917773 100644
--- a/python/tvm/rpc/server.py
+++ b/python/tvm/rpc/server.py
@@ -73,6 +73,13 @@ def _server_env(load_library, work_path=None):
     @tvm._ffi.register_func("tvm.rpc.server.download_linked_module", override=True)
     def download_linked_module(file_name):
         """Load module from remote side."""
+
+        # Guess C compiler[?]
+        if  'CXX' in os.environ.keys():
+            cc=os.environ['CXX']
+        else:
+            cc="g++9" if sys.platform.startswith("freebsd") else "g++"
+    
         # pylint: disable=import-outside-toplevel
         path = temp.relpath(file_name)
 
diff --git a/python/tvm/runtime/module.py b/python/tvm/runtime/module.py
index d9166b5f4..5c86c7bd9 100644
--- a/python/tvm/runtime/module.py
+++ b/python/tvm/runtime/module.py
@@ -17,6 +17,8 @@
 
 # pylint: disable=invalid-name, unused-import, import-outside-toplevel
 """Runtime Module namespace."""
+import sys
+import os
 import ctypes
 import struct
 from collections import namedtuple
@@ -393,13 +395,20 @@ def load_module(path, fmt=""):
     This function will automatically call
     cc.create_shared if the path is in format .o or .tar
     """
+
+    # Guess C compiler[?]
+    if  'CXX' in os.environ.keys():
+        cc=os.environ['CXX']
+    else:
+        cc="g++9" if sys.platform.startswith("freebsd") else "g++"
+    
     # High level handling for .o and .tar file.
     # We support this to be consistent with RPC module load.
     if path.endswith(".o"):
         # Extra dependencies during runtime.
         from tvm.contrib import cc as _cc
 
-        _cc.create_shared(path + ".so", path)
+        _cc.create_shared(path + ".so", path, cc=cc)
         path += ".so"
     elif path.endswith(".tar"):
         # Extra dependencies during runtime.
@@ -408,7 +417,7 @@ def load_module(path, fmt=""):
         tar_temp = _util.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.create_shared(path + ".so", files, cc=cc)
         path += ".so"
     # TODO(weberlo): we should probably use a more distinctive suffix for uTVM object files
     elif path.endswith(".obj"):
-- 
2.17.1

g++ is hardcoded in the current .py files. May be having the option to pass it via environment or a different mechanism would be a good idea.

2 Likes

Using CXX is a good idea IMO. Would you love to send a PR? Thanks!

most of the patches looks great, although we might not want to hardcode g+±9 in freebsd. Reusing CXX env var might be a good idea

Here is an updated patch that is using only ‘CXX’, no more g++9.

From b38755e7b9690e37130a3096aa7a3a6d52fb80e8 Mon Sep 17 00:00:00 2001
From: Mihai M <mmihai@delajii.net>
Date: Mon, 28 Sep 2020 23:59:47 -0700
Subject: [PATCH] Updated runtime to run under FreeBSD - for FreeBSD 12.0
 setenv CXX g++9.

---
 python/tvm/_ffi/libinfo.py   | 2 +-
 python/tvm/contrib/cc.py     | 5 +++--
 python/tvm/rpc/server.py     | 7 +++++--
 python/tvm/runtime/module.py | 9 +++++++--
 4 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/python/tvm/_ffi/libinfo.py b/python/tvm/_ffi/libinfo.py
index b9fc8dc55..ae0f63ea4 100644
--- a/python/tvm/_ffi/libinfo.py
+++ b/python/tvm/_ffi/libinfo.py
@@ -70,7 +70,7 @@ def find_lib_path(name=None, search_path=None, optional=False):
     if os.environ.get("TVM_LIBRARY_PATH", None):
         dll_path.append(os.environ["TVM_LIBRARY_PATH"])
 
-    if sys.platform.startswith("linux"):
+    if sys.platform.startswith("linux") or sys.platform.startswith("freebsd"):
         dll_path.extend(split_env_var("LD_LIBRARY_PATH", ":"))
         dll_path.extend(split_env_var("PATH", ":"))
     elif sys.platform.startswith("darwin"):
diff --git a/python/tvm/contrib/cc.py b/python/tvm/contrib/cc.py
index 1b6a62fd9..add58137a 100644
--- a/python/tvm/contrib/cc.py
+++ b/python/tvm/contrib/cc.py
@@ -17,6 +17,7 @@
 """Util to invoke C/C++ compilers in the system."""
 # pylint: disable=invalid-name
 import sys
+import os
 import subprocess
 
 from .._ffi.base import py_str
@@ -39,7 +40,7 @@ def create_shared(output, objects, options=None, cc="g++"):
     cc : Optional[str]
         The compiler command.
     """
-    if sys.platform == "darwin" or sys.platform.startswith("linux"):
+    if sys.platform == "darwin" or sys.platform.startswith("linux") or sys.platform.startswith("freebsd"):
         _linux_compile(output, objects, options, cc, compile_shared=True)
     elif sys.platform == "win32":
         _windows_shared(output, objects, options)
@@ -103,7 +104,7 @@ def get_target_by_dump_machine(compiler):
 # assign so as default output format
 create_shared.output_format = "so" if sys.platform != "win32" else "dll"
 create_shared.get_target_triple = get_target_by_dump_machine(
-    "g++" if sys.platform == "darwin" or sys.platform.startswith("linux") else None
+    os.environ["CXX"] if "CXX" in os.environ.keys() else "g++" if sys.platform == "darwin" or sys.platform.startswith("linux") else None
 )
 
 
diff --git a/python/tvm/rpc/server.py b/python/tvm/rpc/server.py
index 03a124b46..bdca73e34 100644
--- a/python/tvm/rpc/server.py
+++ b/python/tvm/rpc/server.py
@@ -73,6 +73,9 @@ def _server_env(load_library, work_path=None):
     @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['CXX'] if 'CXX' in os.environ.keys() else "g++"
+
         # pylint: disable=import-outside-toplevel
         path = temp.relpath(file_name)
 
@@ -80,7 +83,7 @@ def _server_env(load_library, work_path=None):
             # Extra dependencies during runtime.
             from tvm.contrib import cc as _cc
 
-            _cc.create_shared(path + ".so", path)
+            _cc.create_shared(path + ".so", path, cc=cc)
             path += ".so"
         elif path.endswith(".tar"):
             # Extra dependencies during runtime.
@@ -89,7 +92,7 @@ def _server_env(load_library, work_path=None):
             tar_temp = util.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.create_shared(path + ".so", files, cc=cc)
             path += ".so"
         elif path.endswith(".dylib") or path.endswith(".so"):
             pass
diff --git a/python/tvm/runtime/module.py b/python/tvm/runtime/module.py
index d9166b5f4..b133036fb 100644
--- a/python/tvm/runtime/module.py
+++ b/python/tvm/runtime/module.py
@@ -17,6 +17,7 @@
 
 # pylint: disable=invalid-name, unused-import, import-outside-toplevel
 """Runtime Module namespace."""
+import os
 import ctypes
 import struct
 from collections import namedtuple
@@ -393,13 +394,17 @@ def load_module(path, fmt=""):
     This function will automatically call
     cc.create_shared if the path is in format .o or .tar
     """
+
+    # c++ compiler/linker
+    cc=os.environ["CXX"] if "CXX" in os.environ.keys() else "g++"
+    
     # High level handling for .o and .tar file.
     # We support this to be consistent with RPC module load.
     if path.endswith(".o"):
         # Extra dependencies during runtime.
         from tvm.contrib import cc as _cc
 
-        _cc.create_shared(path + ".so", path)
+        _cc.create_shared(path + ".so", path, cc=cc)
         path += ".so"
     elif path.endswith(".tar"):
         # Extra dependencies during runtime.
@@ -408,7 +413,7 @@ def load_module(path, fmt=""):
         tar_temp = _util.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.create_shared(path + ".so", files, cc=cc)
         path += ".so"
     # TODO(weberlo): we should probably use a more distinctive suffix for uTVM object files
     elif path.endswith(".obj"):
-- 
2.24.1

To summarize:

  • build (cmake) works w/o problems under FreeBSD (tried under 12.0)
  • runtime needs this patch to enable finding libs and proper g++ binary
  • I’ve tried both runtime and model conversion (relay.frontend.from_tflite). Both worked for me
  • target ‘llvm’ and ‘opencl’ are functional. Using opencl was slower … but it might be my card (RX 560)
  • deploy_lib.tar made under Linux is loadable under FreeBSD - I assume as long it does not require external libs. Also linux will load deploy_lib.tar created under FreeBSD
1 Like

Would you love to send a quick PR on GitHub so that we can merge the patch in? Thanks!

Done. PR #6600

Regards.

1 Like