Skip to content

issue/176: 支持macos编译 #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 63 additions & 8 deletions scripts/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,74 @@

PROJECT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
os.chdir(PROJECT_DIR)
print(f"Changed directory to: {PROJECT_DIR}")

def run_cmd(cmd):
subprocess.run(cmd, text=True, encoding="utf-8", check=True, shell=True)
"""执行命令并添加打印和错误处理"""
print(f"\nExecuting: {cmd}")
try:
subprocess.run(cmd, text=True, encoding="utf-8", check=True, shell=True)
print(f"Successfully executed: {cmd}")
except subprocess.CalledProcessError as e:
print(f"[ERROR] Command failed with return code {e.returncode}: {cmd}")
raise e
except Exception as e:
print(f"[ERROR] An unexpected error occurred while running: {cmd}")
print(f"Error details: {e}")
raise e

def install(user_config_flags_str=""):
"""配置、编译和安装 InfiniCore"""

def install(xmake_config_flags=""):
run_cmd(f"xmake f {xmake_config_flags} -cv")
run_cmd("xmake")
run_cmd("xmake install")
run_cmd("xmake build infiniop-test")
run_cmd("xmake install infiniop-test")
base_flags = ["-c", "-v"]
user_flags_list = user_config_flags_str.split()
platform_flags = []

if platform.system() == "Darwin":
print("[Info] Detected macOS platform.")

# 1. 禁用 OpenMP
# 检查用户是否明确要求开启 omp
user_wants_omp = any(flag == "--omp=y" for flag in user_flags_list)
if user_wants_omp:
print("[Info] User explicitly requested '--omp=y'. Enabling OpenMP (compiler support required).")
else:
# 检查用户是否已明确要求关闭 omp
user_disabled_omp = any(flag == "--omp=n" for flag in user_flags_list)
if not user_disabled_omp:
print("[Info] Defaulting to '--omp=n' on macOS to avoid compiler errors.")
# 避免重复添加
if "--omp=n" not in platform_flags and "--omp=n" not in user_flags_list:
platform_flags.append("--omp=n")
else:
print("[Info] User explicitly requested '--omp=n'. Disabling OpenMP.")

# 合并标志
final_config_flags_list = platform_flags + user_flags_list + base_flags
# 简单的去重
final_config_flags_list = sorted(list(set(final_config_flags_list)), key=final_config_flags_list.index)
xmake_config_command = ["xmake", "f"] + final_config_flags_list

try:
run_cmd(" ".join(xmake_config_command)) # 组合回字符串以适应 shell=True
run_cmd("xmake")
run_cmd("xmake install")
print("\nBuilding test target 'infiniop-test'...")
run_cmd("xmake build infiniop-test")
print("\nInstalling test target 'infiniop-test'...")
run_cmd("xmake install infiniop-test")
print("\n[SUCCESS] InfiniCore installation process completed successfully.")
except Exception as e:
print(f"\n[FAILED] Installation process encountered an error: {e}")
sys.exit(1)

if __name__ == "__main__":
print("--- Starting InfiniCore Installation Script ---")
print("Setting up environment variables...")
set_env()
install(" ".join(sys.argv[1:]))
print("Environment setup complete.")

user_provided_flags = " ".join(sys.argv[1:])
print(f"User provided flags: '{user_provided_flags}'")
install(user_provided_flags)
print("--- InfiniCore Installation Script Finished ---")
76 changes: 56 additions & 20 deletions scripts/set_env.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,61 @@
import os
import platform


def set_env():
if os.environ.get("INFINI_ROOT") == None:
os.environ["INFINI_ROOT"] = os.path.expanduser("~/.infini")

if platform.system() == "Windows":
new_path = os.path.expanduser(os.environ.get("INFINI_ROOT") + "/bin")
if new_path not in os.environ.get("PATH", ""):
os.environ["PATH"] = f"{new_path};{os.environ.get('PATH', '')}"

elif platform.system() == "Linux":
new_path = os.path.expanduser(os.environ.get("INFINI_ROOT") + "/bin")
if new_path not in os.environ.get("PATH", ""):
os.environ["PATH"] = f"{new_path}:{os.environ.get('PATH', '')}"

new_lib_path = os.path.expanduser(os.environ.get("INFINI_ROOT") + "/lib")
if new_lib_path not in os.environ.get("LD_LIBRARY_PATH", ""):
os.environ["LD_LIBRARY_PATH"] = (
f"{new_lib_path}:{os.environ.get('LD_LIBRARY_PATH', '')}"
)
"""
Sets up environment variables for Infini software, supporting Windows, Linux, and macOS.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请按照之前的格式写,不同平台不要串在一起

"""

infini_root_env = "INFINI_ROOT"
if os.environ.get(infini_root_env) is None:
default_infini_root = os.path.expanduser("~/.infini")
os.environ[infini_root_env] = default_infini_root
print(f"[Info] '{infini_root_env}' was not set. Using default: {default_infini_root}")

infini_root = os.environ.get(infini_root_env)
if not infini_root:
raise RuntimeError(f"Could not determine or set {infini_root_env}")

bin_path = os.path.join(infini_root, "bin")
lib_path = os.path.join(infini_root, "lib")

# Update PATH environment variable
path_env_var = "PATH"
current_path = os.environ.get(path_env_var, "")
path_list = [p for p in current_path.split(os.pathsep) if p]
if bin_path not in path_list:
new_path_value = f"{bin_path}{os.pathsep}{current_path}"
os.environ[path_env_var] = new_path_value
print(f"[Info] Added '{bin_path}' to {path_env_var}")

# Handle platform-specific library path environment variables
system = platform.system()

# Linux platform
if system == "Linux":
lib_path_env_var = "LD_LIBRARY_PATH"
current_lib_path = os.environ.get(lib_path_env_var, "")
lib_path_list = [p for p in current_lib_path.split(os.pathsep) if p]
if lib_path not in lib_path_list:
new_lib_path_value = f"{lib_path}{os.pathsep}{current_lib_path}"
os.environ[lib_path_env_var] = new_lib_path_value
print(f"[Info] Added '{lib_path}' to {lib_path_env_var}")

# macOS platform
elif system == "Darwin":
lib_path_env_var = "DYLD_LIBRARY_PATH"
current_lib_path = os.environ.get(lib_path_env_var, "")
lib_path_list = [p for p in current_lib_path.split(os.pathsep) if p]
if lib_path not in lib_path_list:
new_lib_path_value = f"{lib_path}{os.pathsep}{current_lib_path}"
os.environ[lib_path_env_var] = new_lib_path_value
print(f"[Info] Added '{lib_path}' to {lib_path_env_var}")

# Windows platform
elif system == "Windows":
print(f"[Info] On Windows, ensure '{lib_path}' is accessible (e.g., via PATH).")

# Unsupported platforms
else:
raise RuntimeError("Unsupported platform.")
raise RuntimeError(f"Unsupported platform: {system}. Cannot set library path.")

3 changes: 2 additions & 1 deletion src/infiniop-test/include/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Test {
size_t warm_ups, size_t iterations)
= 0;
virtual std::string toString() const = 0;
virtual ~Test() = default;
};

} // namespace infiniop_test::base
Expand All @@ -99,7 +100,7 @@ class Test {
\
std::string toString() const override; \
\
~Test(); \
~Test() final; \
\
private: \
struct Attributes; \
Expand Down
2 changes: 1 addition & 1 deletion src/infiniop/elementwise/cpu/elementwise_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ struct DeviceImpl::Opaque {};

template <typename... Args>
utils::Result<DeviceImpl> DeviceImpl::create(Args &&...args) {
return utils::Result<DeviceImpl>(nullptr);
return INFINI_STATUS_NOT_IMPLEMENTED;
}

// Perform elementwise operation for different input types
Expand Down
11 changes: 10 additions & 1 deletion src/infiniop/ops/gemm/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,16 @@ enum class MatrixLayout : char {
};

class MatmulInfo {
MatmulInfo() = default;
private:
MatmulInfo(BlasMatrix a, BlasMatrix b, BlasMatrix c, size_t m_val, size_t n_val, size_t k_val, size_t batch_val, bool transed)
: a_matrix(std::move(a)),
b_matrix(std::move(b)),
c_matrix(std::move(c)),
m(m_val),
n(n_val),
k(k_val),
batch(batch_val),
is_transed(transed) {}

public:
BlasMatrix a_matrix;
Expand Down
18 changes: 12 additions & 6 deletions src/infiniop/ops/rearrange/cpu/rearrange_cpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@ infiniStatus_t Descriptor::create(
auto handle = reinterpret_cast<device::cpu::Handle *>(handle_);
auto dtype = y_desc->dtype();
auto ndim = y_desc->ndim();
auto shape = y_desc->shape().data();

auto y_shape_vec = y_desc->shape();


CHECK_API_OR(x_desc->dtype(), dtype, return INFINI_STATUS_BAD_TENSOR_DTYPE);
CHECK_API_OR(x_desc->ndim(), ndim, return INFINI_STATUS_BAD_TENSOR_SHAPE);

auto x_shape_vec = x_desc->shape();
for (size_t i = 0; i < ndim; ++i) {
CHECK_API_OR(x_desc->shape()[i], shape[i], return INFINI_STATUS_BAD_TENSOR_SHAPE);
CHECK_API_OR(x_shape_vec.data()[i], y_shape_vec.data()[i], return INFINI_STATUS_BAD_TENSOR_SHAPE);
}

auto dst_strides = y_desc->strides().data();
auto src_strides = x_desc->strides().data();
auto element_size = infiniSizeOf(dtype);
auto y_strides_vec = y_desc->strides();

auto result = utils::RearrangeMeta::create(shape, dst_strides, src_strides, ndim, element_size);

auto x_strides_vec = x_desc->strides();


auto element_size = infiniSizeOf(dtype);
auto result = utils::RearrangeMeta::create(y_shape_vec.data(), y_strides_vec.data(), x_strides_vec.data(), ndim, element_size);
CHECK_RESULT(result);

*desc_ptr = new Descriptor(
Expand Down
3 changes: 1 addition & 2 deletions src/infinirt/infinirt_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
infiniStatus_t freeAsync(void *ptr, infinirtStream_t stream) IMPL;

#define INFINIRT_DEVICE_API_IMPL INFINIRT_DEVICE_API(, )
#define INFINIRT_DEVICE_API_NOOP INFINIRT_DEVICE_API({ return INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED; }, \
{*count = 0; return INFINI_STATUS_SUCCESS; })
#define INFINIRT_DEVICE_API_NOOP INFINIRT_DEVICE_API({ return INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED; }, {*count = 0; return INFINI_STATUS_SUCCESS; })

#endif // __INFINIRT_IMPL_H__
62 changes: 37 additions & 25 deletions test/infiniop/libinfiniop/liboperators.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,48 @@ def __getattr__(self, name):
raise AttributeError(f"Attribute {name} not found in library")


# Open operators library
def open_lib():
def find_library_in_ld_path(subdir, library_name):
ld_library_path = os.path.join(INFINI_ROOT, subdir)
paths = ld_library_path.split(os.pathsep)
for path in paths:
full_path = os.path.join(path, library_name)
if os.path.isfile(full_path):
return full_path
return None

system_name = platform.system()
# Load the library
if system_name == "Windows":
libop_path = find_library_in_ld_path("bin", "infiniop.dll")
librt_path = find_library_in_ld_path("bin", "infinirt.dll")
elif system_name == "Linux":
libop_path = find_library_in_ld_path("lib", "libinfiniop.so")
librt_path = find_library_in_ld_path("lib", "libinfinirt.so")

assert (
libop_path is not None
), f"Cannot find infiniop.dll or libinfiniop.so. Check if INFINI_ROOT is set correctly."
assert (
librt_path is not None
), f"Cannot find infinirt.dll or libinfinirt.so. Check if INFINI_ROOT is set correctly."
def find_libraries_for_platform():
"""查找当前平台的库文件并返回路径"""
system_name = platform.system()

# 确定库文件的目录和名称
if system_name == "Windows":
lib_dir = "bin"
op_lib_name = "infiniop.dll"
rt_lib_name = "infinirt.dll"
elif system_name == "Linux":
lib_dir = "lib"
op_lib_name = "libinfiniop.so"
rt_lib_name = "libinfinirt.so"
elif system_name == "Darwin":
lib_dir = "lib"
op_lib_name = "libinfiniop.dylib"
rt_lib_name = "libinfinirt.dylib"
else:
raise RuntimeError(f"Unsupported operating system: {system_name}")

# 构建完整路径
op_path = os.path.join(INFINI_ROOT, lib_dir, op_lib_name)
rt_path = os.path.join(INFINI_ROOT, lib_dir, rt_lib_name)

# 检查文件是否存在
if not os.path.isfile(op_path):
raise RuntimeError(f"Cannot find the InfiniOP library ({op_lib_name}). Searched in {os.path.join(INFINI_ROOT, lib_dir)}. Check if INFINI_ROOT is set correctly.")
if not os.path.isfile(rt_path):
raise RuntimeError(f"Cannot find the InfiniRT library ({rt_lib_name}). Searched in {os.path.join(INFINI_ROOT, lib_dir)}. Check if INFINI_ROOT is set correctly.")

print(f"[Info] Found libraries in {INFINI_ROOT}/{lib_dir}: {op_lib_name}, {rt_lib_name}")
return op_path, rt_path

# 查找库文件
libop_path, librt_path = find_libraries_for_platform()

librt = ctypes.CDLL(librt_path)
libop = ctypes.CDLL(libop_path)
lib = InfiniLib(librt, libop)


lib.infiniopCreateTensorDescriptor.argtypes = [
POINTER(infiniopTensorDescriptor_t),
c_uint64,
Expand Down