build_libs.py 1.52 KB
Newer Older
zhouxiang's avatar
zhouxiang committed
1
2
3
4
5
import os
import shutil
import platform
import sys
import argparse
6
import glob
zhouxiang's avatar
zhouxiang committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

parser = argparse.ArgumentParser(description='build fastllm libs')
parser.add_argument('--cuda', dest='cuda', action='store_true', default=False,
                    help='build with cuda support')

IS_WINDOWS = (platform.system() == 'Windows')
IS_DARWIN = (platform.system() == 'Darwin')
IS_LINUX = (platform.system() == 'Linux')

BUILD_DIR = 'build-py' # build path

def build_libs():
    # create build dir
    root_dir = os.path.dirname(os.getcwd())
    cmake_build_dir = os.path.join(root_dir, BUILD_DIR)
    if os.path.exists(cmake_build_dir):
        shutil.rmtree(cmake_build_dir)
    os.makedirs(cmake_build_dir)
    os.chdir(cmake_build_dir)

27
28
    # build it
    cpu_num = min(os.cpu_count(), 4)
zhouxiang's avatar
zhouxiang committed
29
30
    args = parser.parse_args()
    if IS_WINDOWS:
31
        os.system('cmake -G Ninja -DPY_API=ON .. && ninja pyfastllm')
zhouxiang's avatar
zhouxiang committed
32
33
34
    elif IS_LINUX:
        extra_opts = ' -DPY_API=ON '
        extra_opts += ' -DUSE_CUDA=ON ' if args.cuda else ' '
35
        build_cmd = f"cmake {extra_opts} .. && make pyfastllm -j{cpu_num}"
zhouxiang's avatar
zhouxiang committed
36
        print(build_cmd)
37
        os.system(f"cmake {extra_opts} .. && make pyfastllm -j{cpu_num}")
zhouxiang's avatar
zhouxiang committed
38
39
    else:
        extra_opts = '-DPY_API=ON'
40
41
42
43
44
        os.system(f"cmake {extra_opts} .. && make pyfastllm -j{cpu_num}")
    
    so_files = glob.glob("*.so", root_dir=cmake_build_dir)
    for file in so_files:
        shutil.copy(os.path.join(cmake_build_dir, file), os.path.join(root_dir, "pyfastllm/fastllm"))
zhouxiang's avatar
zhouxiang committed
45
46

if __name__ == '__main__':
47
    build_libs()